rizzy818 Posted February 5, 2016 Share Posted February 5, 2016 I've been playing around with the tutorials and trying to understand phaser. i made some changes to the http://phaser.io/examples/v2/arcade-physics/vertical-collision What i wanted to do is make one of the sprites draggable so that i can do something when it collides with the other sprite. i changed the parameters of collide because in a previous post someone mentions that since i'm moving the sprite i need to check for overlap. In my collisionHandler function i just wanted to know it collided by putting some text on the screen and chaging the background to red (like in the example). The issue i get is that the text does appear on the screen but the background doesn't turn red. below is the code to the two functions function update() { game.physics.arcade.collide(sprite1, sprite2, collisionHandler, collisionHandler, this); } function collisionHandler (obj1, obj2) { result = "Collision Detected!!!"; game.stage.backgroundColor = '#992d2d'; } function render() { game.debug.text(result, 10, 20); game.debug.body(sprite1); game.debug.body(sprite2); } Link to comment Share on other sites More sharing options...
rizzy818 Posted February 10, 2016 Author Share Posted February 10, 2016 just a quick follow up attached is a screen shot of what happens when there is a collision. Here is the code also. var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.image('grid', 'assets/tests/debug-grid-1920x1920.png'); game.load.image('atari', 'assets/sprites/jordan3.png'); game.load.image('sonic', 'assets/sprites/shopping_bag.png'); game.load.image('shoe', 'assets/sprites/jordan3.png'); game.load.image('mushroom', 'assets/sprites/shopping_bag.png'); } var result = 'Drag a sprite'; var sprite1; var sprite2; function create() { game.physics.startSystem(Phaser.Physics.ARCADE); //game.stage.backgroundColor = '#ffffff'; game.add.sprite(0, 0, 'grid'); //var atari = game.add.sprite(0, 50, 'atari'); // Enable input and allow for dragging /* atari.inputEnabled = true; atari.input.enableDrag(); atari.events.onDragStart.add(onDragStart, this); atari.events.onDragStop.add(onDragStop, this); var sonic = game.add.sprite(640, 50, 'sonic'); sonic.inputEnabled = true; sonic.input.enableDrag(); sonic.events.onDragStart.add(onDragStart, this); sonic.events.onDragStop.add(onDragStop, this);*/ sprite1 = game.add.sprite(300,50, 'shoe'); sprite1.name = 'shoe'; //sprite1.body.setSize(220, 10, 0, 0); game.physics.enable(sprite1, Phaser.Physics.ARCADE); sprite1.body.setSize(300, 50, 0, 100); sprite1.inputEnabled = true; sprite1.input.enableDrag(); sprite1.events.onDragStart.add(onDragStart, this); sprite1.events.onDragStop.add(onDragStop, this); sprite2 = game.add.sprite(355, 350, 'mushroom'); sprite2.name = 'mushroom'; game.physics.enable(sprite2, Phaser.Physics.ARCADE); sprite2.body.immovable = true; // //sprite2.body.velocity.y = 100; // sprite2.inputEnabled = true; // sprite2.input.enableDrag(); // sprite2.events.onDragStart.add(onDragStart, this); // sprite2.events.onDragStop.add(onDragStop, this); } function onDragStart(sprite, pointer) { result = "Dragging " + sprite.key; } function onDragStop(sprite, pointer) { result = sprite.key + " dropped at x:" + pointer.x + " y: " + pointer.y; } function update() { game.physics.arcade.collide(sprite1, sprite2, collisionHandler, collisionHandler, this); } function collisionHandler (obj1, obj2) { result = "Collision Detected!!!"; game.stage.backgroundColor = '#992d2d'; } function render() { game.debug.text(result, 10, 20); game.debug.body(sprite1); game.debug.body(sprite2); //game.debug.body(atari); } Link to comment Share on other sites More sharing options...
Recommended Posts