yes, exactly. It should be like agar.io
The only issue is that it’s different when starting the scene a second time.
yes, exactly. It should be like agar.io
The only issue is that it’s different when starting the scene a second time.
Here is what you should do (and explanations of why ):
BTW this isn’t in ts so you’l have to adjust it.
To get the fish to move towards the pointer, you can use Arcade physics’s built in moveTo
command. Your code should look like this:
this.input.on('pointermove', function(pointer) {
this.physics.moveTo(player, pointer.x, pointer.y, speed);
}
speed
should be a number between 200-1000.
To add boosting, I would make a variable which tells us if or if not we can boost. This will be using a spacebar listener.
this.input.keyboard.on('keydown-' + 'SPACE', function () {
if(boostable === true) {
boostable = false;
this.physics.moveTo(player, pointer.x, pointer.y, speed+300); // not sure this will work, we'll see
setTimeout(()=>{
player.body.reset(player.x,player.y); // moveTo never stops so we have to cancel the above
}, 500);
setTimeout(()=>{
boostable = true; // allows boosting again
}, 2000);
});
500 is the length of the boosting time.
2000 is the amount of time until you can boost again.
Make sure you define boostable
somewhere in create() and make sure it is set to true in the beginning.
BTW I think your player is called playerFish so make sure to account for that.
Thanks @UltimateProGrammer!
Will this work when restarting the scene though? What did this have to do with the creation of the scene that makes it fix the issue?
Hopefully it will work when restarting the scene, I have no reason to think why it wouldn’t. I don’t know what it has to do with scene creation but the first thing you should do is make sure it is the Phaser way to do it.
I think the “cannot cut frame” error has to do with me setting the main camera to follow a sprite and then destroying the scene. For some reason when I tell the camera to follow the fish in the new scene it just blows up…
I ams tarting the camera following the player like this:
this.cameras.main.startFollow(playerFish)
But strangely, when I try to call
this.cameras.main.stopFollow()
I get the error, “Cannot read property ‘stopFollow’ of undefined”
this.cameras.main.stopFollow()
Cannot read property ‘stopFollow’ of undefined
That probably means that you are using this
out of scope. Pass this
as an argument to wherever you are saying that.
thanks. Turns out when I went to the game over screen my game logic was setting size to 0, and it would then set the playerFish’s body to a size of zero.
Then when I set the camera to follow this sprite with zero size body the camera totally freaked out! No errors in the console made this much harder to debug.
(my bad, but it would nice if this situation would throw an error rather than the current behavior!)
I already had the same problem in my game.
You don’t need remove the scene.
The problem here is yours event listeners.
Every time you call ’this.scene.start’ the current scene is closed and all sprites destroyed.
Just remove the events when destroy the sprites.
There are a bulletin function for that.
I advised too don’t use window.setTimeout. Is preferred use this.scene.time.addEvent.
p.s. English is not my first language
EDIT:
See my other post about NEVER using setTimeout() here.
Why not use setTimeout? It’s not very Phaser-like but it takes way less time to write.
Edit 2:
The answer to the above question is explained thoroughly on this post.
I think phaser cleanup events made with time.addEvent when destroying a scene.