Valafar Posted July 29, 2015 Share Posted July 29, 2015 How do I reset a sprite to its original position? I've been trying to follow the tutorials but couldn't find a solution. I know there is Sprite.reset method, but when I try to use it, the sprite just "flashes" quickly at the intended position and then disappears. The idea is that a when mySprite hits a sensor, it will be reset to its starting position waiting for user to start a new round. Below is some relevant code. Also if there's something unrelated I'm doing wrong, please let me know.var game = new Phaser.Game('100%', '100%', Phaser.AUTO, 'app', { preload: preload, create: create, update: update, render: render});var endSensorFixture;var map;var mySprite;var mySpriteStartingPoint;function preload() { game.load.text('map', 'map.json'); // Exported from RUBE. Contains all sprites and the game map.}function create() { game.physics.startSystem(Phaser.Physics.BOX2D); game.physics.box2d.setPTMRatio(38); game.input.addMoveCallback(dragMove); map = new Phaser.Physics.Box2D.RubeScene('map'); // Load RubeScene and initialize Box2D elements map.load(0, 0, function () { mySprite = map.getSprite('mySprite'); mySpriteStartingPoint = new Phaser.Point(mySprite.x, mySprite.y); mySprite.inputEnabled = true; //mySprite.input.enableDrag(); // does not work mySprite.body.data.SetActive(false); mySprite.events.onInputDown.add(dragStart, mySprite); mySprite.events.onInputUp.add(dragEnd, mySprite); endSensorFixture = map.getFixture('endSensorFixture'); mySprite.body.setFixtureContactCallback(endSensorFixture, onHitEndSensor, mySprite); });}function update() { updateRubeSprites();}function onHitEndSensor(body1, body2, fixture1, fixture2, begin) { endGame();}function endGame() { if (isGameRunning) { mySprite.reset(mySpriteStartingPoint.x, mySpriteStartingPoint.y); mySprite.body.data.SetActive(false); }} function dragStart() { game.physics.box2d.mouseDragStart({ x: -game.camera.x - game.input.mousePointer.x, y: game.camera.y + game.input.mousePointer.y });}function dragMove() { game.physics.box2d.mouseDragMove({ x: -game.camera.x - game.input.mousePointer.x, y: game.camera.y + game.input.mousePointer.y });}function dragEnd() { game.physics.box2d.mouseDragEnd();}I'm using Phaser 2.4.2, Box2D plugin 1.0.2 and RUBE loader. Link to comment Share on other sites More sharing options...
CodeToWin Posted July 29, 2015 Share Posted July 29, 2015 you can just set the sprite.body.x and sprite.body.y to whatever you want it to be. Link to comment Share on other sites More sharing options...
Valafar Posted July 30, 2015 Author Share Posted July 30, 2015 you can just set the sprite.body.x and sprite.body.y to whatever you want it to be. Same effect, i.e. a quick "flash" and then disappearance. I'm starting to suspect that there is something wrong with the RUBE Loader, but can't quite figure out what... Link to comment Share on other sites More sharing options...
Recommended Posts