jschomay Posted July 15, 2014 Share Posted July 15, 2014 I'm building a game where you climb higher and higher, one level at a time. The world is potentially infinite, so I can't set world bounds and move the camera. Also, the world should only scroll when the player is climbing, so I can't just add a constant vertical velocity to all my objects. Instead, I'm trying to move everything in the world down (except for the player) during a climb phase:Player.prototype.climb = function(gameState) { var climbTime = 700; var climbSpeed = gameState.getScaffoldHeight()*1000/climbTime; this.climbing = true; this.body.allowGravity = false; this.body.velocity.y -= climbSpeed/6; this.game.world.setAllChildren('body.velocity.y', climbSpeed, true, true, 1, false); stopClimbing = function() { this.climbing = false; this.body.velocity.y = 0; this.body.allowGravity = true; this.game.world.setAllChildren('body.velocity.y', climbSpeed, true, true, 2, false); }; this.game.time.events.add(climbTime, stopClimbing, this);};This code works for the most part. But there is a problem with `world.setAllChildren`. It seems to be changing the velocities on my sprites that are part of a group, but not on any individual sprites. Individual sprites just stay fixed to the screen. Note that everything has the arcade physics enable on it. I have tried changing the `allowGravity` and `inmovable` properties around, but it doesn't seem to change anything. Also, if I manually add a line like `gameState.mySingleSprite.body.velocity.y += climbSpeed` in the mix it works as expected. But I don't want to have to do that for each object. Is my approach wrong? Do I have an error? Is there a better way? Also, there are some objects (like score and hud) that I won't want to move via setAllChildren - is there a way to put some property on them to keep them fixed? I would imagine they would stay in place if they don't have physics turned on for them, but I don't have a way to get a false positive on that hypothesis at the moment. Thanks in advance for your help. This is my first game with Phaser and I am really liking it, but I feel I may be missing some general things. Link to comment Share on other sites More sharing options...
jschomay Posted July 16, 2014 Author Share Posted July 16, 2014 AHA! Fixed. After some playing around, I realised that setting the `checkAlive` parameter of `setAllChildren` to false instead of true made it work for changing alpha properties, though it gave an error if I tried that with the body.velocity property, which is why I set it to true in the first place. I realised the reason was because my text objects didn't have physics, and hence no bodies, hence the error. The crucial step was to set the alive property to true for everything I wanted to scroll. Interestingly, my groups were scrolling because I'm using an object pool, so all sprites in my groups were `revive()`-ed, while nothing else was. TL;DR - if you use the `checkAlive` flag, make sure everything you want affected by `setAllChildren` is actually alive. As a bonus, this is a nice way to selectively "fix" some objects to the screen. Link to comment Share on other sites More sharing options...
Recommended Posts