eros Posted May 24, 2015 Share Posted May 24, 2015 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? Quote Link to comment Share on other sites More sharing options...
PixelPicoSean Posted May 24, 2015 Share Posted May 24, 2015 Your variables are not acceptable for startParallax function, simply add them as properties of the scene:// Inside the init functionthis.backgroundTrees = this.addParallax('tree-3.png', 400, 0);// Change its speed laterstartParallax: function() { this.backgroundTrees.speed.x = -100;} SkyzohKey 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.