jaymeh Posted September 21, 2017 Share Posted September 21, 2017 Hi, I have been working on my first full game and decided to try a snake clone. I haven't followed any kind of tutorial and have just been trying to get to grips with the framework. I am however having a few issues and have been stuck for a couple of hours to try and fix a couple of annoying bugs. The first main issue I have with my game is that collisions don't seem to be firing correctly. I have tried to set one up when the head of the snake collides with any of the tail elements which just restarts the game state for testing purposes. This seems to however try to trigger another function which should only happen when a player collides with food. I am guessing I need to setup my collision handlers differently in order to fix this. The other bug is that when a bit of food has been eaten a new sprite it created but just before it does it gets added randomly to somewhere on the screen then disappears but I can't work out why that would be. I setup a git repo of my project so far on github and it can be found here: https://github.com/jaymeh/phaser-snake If someone could offer some advice as to what I can do to fix this I would be very grateful. Thanks Link to comment Share on other sites More sharing options...
8Observer8 Posted September 21, 2017 Share Posted September 21, 2017 Your can create a repository on github with the name: jaymeh.github.io It will be a hosting for your games. Link to comment Share on other sites More sharing options...
samid737 Posted September 21, 2017 Share Posted September 21, 2017 The first bug is is caused by the tail array. When you restart and _generateFood again the first time, the tail.length will be incorrect (if you ate at least one food in the previous turn). When you call _trigger_death(), the game state restarts but tail is not reset (you have to manage that manually in your case). try: function _trigger_death(one, two) { tail=[]; game.state.restart(); } Here is a related topic to deal with this. The second bug is caused by _eat_food() . When the next sprite is added after eating, the nextY and nextX values are calculated incorrectly probably (try 100,100 and it will always spawn at 100,100). A simple way out is to set nextX and nextY to -100 so that you won't see that happening. jaymeh 1 Link to comment Share on other sites More sharing options...
jaymeh Posted September 22, 2017 Author Share Posted September 22, 2017 I see, I think I assumed that restarted the state would reset the variables at the top of the page but I guess those are not setup by Phaser. I guess it would work if I set them up in a global scope then set their default values inside the create method. Thanks, it's a good thing to note. I see, yeah it seems like I place it in the wrong place but didn't notice it while I was debugging. Thanks samid737, appreciate your help on this! Link to comment Share on other sites More sharing options...
8Observer8 Posted September 22, 2017 Share Posted September 22, 2017 Step by step instruction about making a Snake Game in Phaser: Making Your First HTML5 Game With Phaser --> Click to play <-- Link to comment Share on other sites More sharing options...
jaymeh Posted September 24, 2017 Author Share Posted September 24, 2017 Thanks 8Observer8, Handy tutorial and good to refer to, for the bits I was struggling with. 8Observer8 1 Link to comment Share on other sites More sharing options...
Recommended Posts