Froton X Posted March 18, 2014 Share Posted March 18, 2014 Hey, I'm working on the cloud9 ide, and in the preview, the sprite remains static, it is not falling and the controls set are not controlling it either. I've attached a screenshot and pasted the code. var dragonSprite;var game = new Phaser.Game (800, 600, Phaser.AUTO, '',{ preload: preload, create: create, update: update });function preload() {game.load.image('dragon', 'Dragon.png');}function create() {dragonSprite = game.add.sprite(game.world.centerX,500, 'dragon');dragonSprite.acceleration.y = 100; // gravitydragonSprite.body.collideWorldBounds = true;dragonSprite.body.drag.x = 0;dragonSprite.anchor.setTo('.5, .5');}function update() {if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {dragonSprite.velocity.x = -125;dragonSprite.scale.x = 1;} else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {dragonSprite.velocity.x = 125;dragonSprite.scale.x = -1;}}if(game.input.keyboard.isDown(Phaser.Keyboard.UP)) {dragonSprite.velocity.y = -100;}Anything wrong with the code? I changed the y position of the character later from 0 to 500 so now they are near the bottom without touching any border and it still won't move. Am I doing something wrong? Did I pick a bad time to start learning since I'm seeing the 2.0 is out and all. O -0 Link to comment Share on other sites More sharing options...
Chupup Games Posted March 18, 2014 Share Posted March 18, 2014 You need to set a physics system for your sprite to use gravity and accleration, look in the API for Arcade, Ninja or P2 (the 3 different Physic engines that are already implemented in Phaser) Link to comment Share on other sites More sharing options...
rich Posted March 18, 2014 Share Posted March 18, 2014 Assuming you are using Phaser 2 look at the Arcade Physics examples, specifically how they start the physics system running and then enable a body for it. Link to comment Share on other sites More sharing options...
Froton X Posted March 18, 2014 Author Share Posted March 18, 2014 Thanks Chupup games and Rich. Yes, I'm using Phaser 2, which I downloaded a few days ago. I looked at the examples of how the physics is enabled under game create. I used the line of code to enable it in my game at line 18 and now I just get a white screen. EDIT: I got it to show up again, but the Dragon still doesn't move. Even with arcade physics enabled. Might it be my computer? EDIT 2: Nevermind! I'm guessing the method I'm using is causing errors. I was using a method where all the game data was in a javascript file and would be referenced by the html file. If I use the method in the Phaser websites tutorial, after checking how its organized and testing it myself, it works. Before finding that out, I had made the official tutorial in the other method and only got a black screen. So my question is, everything goes in the html file and the phaser.js is all that is needed to be referenced instead of making other .js files. Or is it just the tutorial that has everything in it and the proper way to do it is to have most of the game stuff in a js file with the html mostly empty except for the script lines? Here is the script:var dragonSprite;var game = new Phaser.Game(800, 600, Phaser.AUTO, '',{ preload: preload, create: create, update: update });function preload() {game.load.image('dragon', 'Dragon.png');}function create() {dragonSprite = game.add.sprite(game.world.centerX,0, 'dragon');dragonSprite.acceleration.y = 100; // gravitydragonSprite.body.collideWorldBounds = true;dragonSprite.body.drag.x = 100;dragonSprite.anchor.setTo(.5, .5);game.physics.enable(dragonSprite, Phaser.Physics.ARCADE);}function update() {if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {dragonSprite.velocity.x = -125;dragonSprite.scale.x = 1;} else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {dragonSprite.velocity.x = 125;dragonSprite.scale.x = -1;}if(game.input.keyboard.isDown(Phaser.Keyboard.UP)) {dragonSprite.velocity.y = -100;} Link to comment Share on other sites More sharing options...
rich Posted March 18, 2014 Share Posted March 18, 2014 I think you've figured this out already, but: You have to enable physics on a sprite BEFORE setting anything like acceleration, velocity, etc. Otherwise it has no body to operate on and will just crash. To answer your question: Most people split their games up, so each JS file contains a small part of it. But for learning, and the Examples, it's easier to do it all in one. Link to comment Share on other sites More sharing options...
Froton X Posted March 19, 2014 Author Share Posted March 19, 2014 (edited) I hadn't until now, although I did learn a ton of other stuff thanks to the documentation and examples. Thanks for building such an incredible framework, Rich. Also, thank you for taking the time to answer mine and other people's questions. I'll try not to ask anything trivial unless it's some kind of apparent error. By the way, are most docs and tutorials useable with 2.0 or should I wait until a 2.0 is released and learn from the examples page for now? EDIT: I forgot to mention, I did figure out all the other stuff I did wrong and how to get stuff to work eventually and now I'm looking forward to being able to make a true html5 game soon. Edited March 19, 2014 by Fradno Link to comment Share on other sites More sharing options...
rich Posted March 19, 2014 Share Posted March 19, 2014 The docs are all fully up to date for 2.0, but the tutorials still need converting, sorry. Link to comment Share on other sites More sharing options...
Froton X Posted March 21, 2014 Author Share Posted March 21, 2014 No problem, for now I'll use the migration guide alongside the older tutorials. Unless this isn't recommended? Keep up the good work, the updates are really fast! Link to comment Share on other sites More sharing options...
Recommended Posts