Roilen Posted March 17, 2016 Share Posted March 17, 2016 (edited) Hello! I'm trying Phaser features, as I need pretty simple game. My game objects (sprites) can be dragged and should be collided with each other and scene bounds. And I'm stuck with the World bounds. When I use game physics (acceleration, and so on), sprite correctly bounces off the World bounds, but when I drag the object - it completely ignores the bounds! I've googled it for several hours, but still confused. Can you please help me? Here is the link to the code in Sandbox: http://phaser.io/sandbox/GcRbUkDx Here is the link to the game in Sandbox: http://phaser.io/sandbox/GcRbUkDx/play And here is the code: function preload() { game.load.baseURL = 'http://examples.phaser.io/assets/'; game.load.crossOrigin = 'anonymous'; game.load.image('phaser', 'sprites/phaser-dude.png'); } function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = 0x5d5d5d; game.physics.arcade.setBoundsToWorld(); game.world.setBounds(0, 0, 800, 600); var sprite = game.add.sprite(0, 0, 'phaser'); game.physics.arcade.enable(sprite); sprite.inputEnabled = true; sprite.input.enableDrag(); sprite.body.moves = false; sprite.body.collideWorldBounds = true; } function update() { } Edited March 18, 2016 by Roilen Solved Link to comment Share on other sites More sharing options...
Roilen Posted March 18, 2016 Author Share Posted March 18, 2016 I've solved the problem and it was pretty easy. RTFM, as usual The solution to the dragging bounds is explained in this tutorial: http://phaser.io/examples/v2/input/bounds-rect Each sprite has the following property: sprite.input.boundsRect = PHASER.Rect; This property is responsible for creating bounds in which a sprite can be dragged, just as it stated in docs: http://phaser.io/docs/2.4.2/Phaser.InputHandler.html#boundsRect So, I've updated the code in Sandbox: http://phaser.io/sandbox/GcRbUkDx And here is the working code: function preload() { game.load.baseURL = 'http://examples.phaser.io/assets/'; game.load.crossOrigin = 'anonymous'; game.load.image('phaser', 'sprites/phaser-dude.png'); } function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = 0x5d5d5d; game.physics.arcade.setBoundsToWorld(); game.world.setBounds(0, 0, 800, 600); //Create new world bounds. var bounds = new Phaser.Rectangle(100, 100, 500, 400); // Create a graphic so you can see the bounds var graphics = game.add.graphics(bounds.x, bounds.y); graphics.beginFill(0x000077); graphics.drawRect(0, 0, bounds.width, bounds.height); var sprite = game.add.sprite(200, 200, 'phaser'); game.physics.arcade.enable(sprite); sprite.inputEnabled = true; sprite.input.enableDrag(); //Enable sprite property for being dragged only whithin specified bounds sprite.input.boundsRect = bounds; //Disable sprity physics, for tests, so it won't be moved by physics sprite.body.moves = false; } function update() { } function render() { } drhayes 1 Link to comment Share on other sites More sharing options...
Recommended Posts