Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Billy B.
    @Azurasky1
    how do i go about doing this and getting them in a bodies array
    basically i can't access variables from inside app.js when i'm using the init() function inside of this module
    Andrea Sonny
    @andreasonny83
    are you creating a new module?
    Billy B.
    @Azurasky1
    no this is player.js
    Player.prototype.init = function(game, avatar, playerInfo) {
        var colVariation = Math.floor(Math.random() * 9);
        var keys = Object.keys(modules.Utils.colors());
    
        _game = game;
    
        _game.player.health = 100;
        _game.player.avatar = new Image();
        _game.player.avatar.src = avatar;
        _game.player.ticks = 2;
        _game.player.tick = this.ticks;
    
        // Pick a random color to assign to the player
        _game.player.color = modules.Utils.colors()[
            keys[keys.length * Math.random() << 0]
          ][
            colVariation
          ];
    
        // place the player in a random place
        _game.player.pos = {
          x: Math.floor((Math.random() * 600) + 100) * _game.scaleFactor,
          y: Math.floor((Math.random() * 400) + 100) * _game.scaleFactor
        };
    
        _game.player.animation = {
          x: 0,
          y: 0
        };
    
        _game.player.frame = {
          current: 0,
          direction: Math.floor((Math.random() * 3)),
          total: playerInfo.frames,
          width: (playerInfo.width / playerInfo.frames),
          height: (playerInfo.height / playerInfo.frames)
        };
    
        _game.player.avatar.onload = function() {
          _log('Player ready');
          document.dispatchEvent(event);
        };
      };
    you see how a local variable _game is assigned to the app.game object
    Andrea Sonny
    @andreasonny83
    so you can use _game
    Billy B.
    @Azurasky1
    no i can't
    Andrea Sonny
    @andreasonny83
    why
    Billy B.
    @Azurasky1
    because it's a local variable and i need to be able to access it elsewhere
    Andrea Sonny
    @andreasonny83
    it is pointing to the global one. that is copied by reference not by value
    when you edit _game you edit app.game
    is just because _game is shorter than app.game so I created a shorter nickname for that
    Billy B.
    @Azurasky1
    that is good to know
    thank you
    Andrea Sonny
    @andreasonny83
    var obj = {};
    var obj2 = obj;
    obj2.player = ‘myPlayer’;
    
    console.log(obj); // {player: ‘myPlayer’}
    objects are always copied by reference in javascript
    you can try by yourself
    try to really clone an object in javascript. is really tricky
    Billy B.
    @Azurasky1
    i think that is one of the topics that was covered in the udemy course
    by reference v.s. by value
    Andrea Sonny
    @andreasonny83
    yes
    Billy B.
    @Azurasky1
    goes to show how much i remembered... lol
    i was thinking of going through it again and taking notes
    Andrea Sonny
    @andreasonny83
    but if you really want to create a copy of that object, is really not easy in javascript
    this is what you need to write if you really want to clone an object to store in a different variable:
    function cloneObject(obj) {
        var clone = {};
        for(var i in obj) {
            if(typeof(obj[i])=="object" && obj[i] != null)
                clone[i] = cloneObject(obj[i]);
            else
                clone[i] = obj[i];
        }
        return clone;
    }
    Billy B.
    @Azurasky1
    jeez lol
    Andrea Sonny
    @andreasonny83
    not really 1 line of code
    Billy B.
    @Azurasky1
    so all i have to do to edit an object from another file is pass it in as a parameter to a function and create a local variable
    Andrea Sonny
    @andreasonny83
    yes
    that is just passing over the address in where the object is stored
    but, if you pass a variable instead of an object, then you will create a new variable
    Billy B.
    @Azurasky1
    does this count as an object
    var bodies = [];
    Andrea Sonny
    @andreasonny83
    var a = 10;
    var b = a;
    b = 20;
    
    console.log(a); // 10
    yes, arrays are special kind of objects
    Billy B.
    @Azurasky1
    so it's by reference too?
    Andrea Sonny
    @andreasonny83
    yes
    Billy B.
    @Azurasky1
    ok
    Andrea Sonny
    @andreasonny83
    you can create a javascript sandbox in where you can run simple tests
    Billy B.
    @Azurasky1
    i guess i might as well include bodies [] within game {}
    Andrea Sonny
    @andreasonny83
    just create a new sandbox.js file and then you can run from your terminal with: node sandbox.js
    Billy B.
    @Azurasky1
    awesome
    i'll do that
    Andrea Sonny
    @andreasonny83
    write this code inside:
    var myVar = [];
    var _var = myVar;
    
    _var.push({
      player: {
        name: 'Joe'
      }
    });
    
    
    console.log(myVar);
    myVar will print what you put inside _var
    so, if you want to push objects inside your array, just use push and pass the object inside the brackets
    Billy B.
    @Azurasky1
    what is this
      modules.Projectiles = (function() {
        return {
          projectiles: [],
          new: function(cv, x, y, width, height, direction, speed) {
            this.projectiles.push(
              new Projectile(cv, x, y, width, height, direction, speed));
          },
    
          getActive: function() {
            return this.projectiles.length;
          },
    
          draw: function() {
            for (var i = 0; i < this.projectiles.length; i++) {
              this.projectiles[i].draw();
            }
          }
        };
    Andrea Sonny
    @andreasonny83
    is the projectile service