Jump to content

Sprite doesn't move or fall

Froton X

Recommended Posts

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

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

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

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 by Fradno
Link to comment
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...