Jump to content

Referencing Variables Instantiated in init() in Custom Functions


eros
 Share

Recommended Posts

I am having some trouble getting my parallax layers to start moving on the x-axis once the player presses right or left. In my init() function in main.js I have added parallax layers via the following code:

var backgroundTrees = this.addParallax('tree-3.png', 400, 0);var midgroundTrees = this.addParallax('tree-2.png', 300, 0,);var foregroundtrees = this.addParallax('tree.png', 200, 0);

the addParallax method:

addParallax: function(texture, pos, speed) {            var sprite = new game.TilingSprite(texture, game.system.width);            sprite.speed.x = speed;            sprite.position.y = game.system.height - sprite.height - pos;            this.addObject(sprite);            this.stage.addChild(sprite);            return sprite;},

the keydown function:

keydown: function(key) {            if (key === "RIGHT" || key === "LEFT") {                this.player.run(key);                this.startParallax();            }},

the startParallax function:

startParallax: function() {            backgroundTrees.speed.x = -100;            midgroundTrees.speed.x = -200;            foregroundTrees.speed.x = -300;}

Basically, when the user presses right or left, the animation of the player changes to running. In addition to this, I am trying call a custom function, startParallax(), that will set the x-speed of the parallax layers to the desired speed. When I run this I receive the following errors in the console:

 

"ReferenceError: backgroundTrees is not defined"

 

If I comment out that line, it will display the same message for midgroundTrees, etc.

 

What do I need to do in order to reference the parallax sprites in order to get them to start moving? Can it be in a custom function as described above?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...