Search the Community
Showing results for tags 'overlap sprite'.
-
Hi This is my code var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'gamediv', { preload: preload, create: create, update: update });var sprite1;var sprite2;//yArray contains the 4 possible positions for a sprite//all must be used once and no more.var yArray = [50, 200, 350, 500];var y2Array = [50, 200, 350, 500];//var rand = reroll();ys1Array = Phaser.Math.shuffleArray(yArray);ys2Array = Phaser.Math.shuffleArray(y2Array);var leftSideSprites = [];var rightSideSprites = [];var leftSpriteArray = [];var rightSpriteArray = [];function preload() { game.load.image('teaspoon', 'teaspoon.png'); game.load.image('teacup', 'teacup.png'); game.load.image('shoes', 'shoes.png'); game.load.image('socks', 'sock.png'); game.load.image('television', 'television.png'); game.load.image('remote', 'remote.png'); game.load.image('hat', 'hat.png'); game.load.image('head', 'head.png');};function create() { leftSideSprites.push('teacup', 'shoes', 'television', 'head'); rightSideSprites.push('teaspoon', 'socks', 'remote', 'hat'); game.stage.backgroundColor = '#ffffff'; // Creating 8 sprites, randomly placing them and then assigning IDs to them. for (i = 0; i <= 3; i ++) { var spriteName = "sprite" + i; spriteName = game.add.sprite(50, ys1Array[i], leftSideSprites[i]); spriteName.id = i; game.physics.enable(spriteName, Phaser.Physics.ARCADE);// Enabling input of each sprite spriteName.inputEnabled = true; spriteName.input.enableDrag(); spriteName.events.onDragStart.add(startDrag, this); spriteName.events.onDragStop.add(stopDrag, this); leftSpriteArray.push(spriteName); } for (var i = 0; i <=3; i++) { var spriteName = "sprite" + i; spriteName = game.add.sprite(670, ys2Array[i], rightSideSprites[i]); spriteName.id = i; game.physics.enable(spriteName, Phaser.Physics.ARCADE); rightSpriteArray.push(spriteName); } console.log(leftSpriteArray[2]); console.log(rightSpriteArray[2].id);};function update() {};function overlapHandler (clickedSprite) { console.log("SUCCESS");}function startDrag(clickedSprite) { console.log('the id of the sprite I clicked = ' + clickedSprite.id); sprite1 = leftSpriteArray[clickedSprite.id]; sprite2 = rightSpriteArray[clickedSprite.id];}function stopDrag(clickedSprite) { game.physics.arcade.overlap(sprite1, sprite2, overlapHandler(clickedSprite) , null, this); }Basically the overlap handler is called any time I drag the sprite even though it doesn't collide with anything. Is there anyway for it to be called only when two specific sprites overlap.