Sign in to follow this  
billmo

Sprite Collision On Creation Check

Recommended Posts

Hey Guys,

 

New to Phaser and need help in an issue I'm running into. I'm trying to randomly create a sprite (the coin) in my stage but want to check if it's overlapping with a particular tile before showing. If it does overlap, I want to kill it and create it in a new position. I don't even know if this is the best approach, but it's what I've been trying.  So far, I can't get it to work. The sprite has no issues being created overlapping the tile I've been trying to have it avoid.

 

Here's my code so far:

window.onload = function() {        var game = new Phaser.Game(640, 640, Phaser.AUTO, '', { preload: preload, create: create, update: update });        var map;        var heart;        var backgroundLayer;        var blocks;        var player;        var coin;        var jumptimer = 0;        var arrow;        var arrows;        var arrowTime = 0;        var score_counter = 0;        var remaining_arrows = 40;        var score_text;        var arrows_text;        var last_clicked;        var new_remaining_arrows;        function preload () {            game.load.tilemap('myMap', 'tilemaps/test_tilemap.json', null, Phaser.Tilemap.TILED_JSON);            game.load.image('sheet', 'tilemaps/scifi_platformTiles_32x32.png');            game.load.spritesheet('dude', 'images/bear-dude.png', 36, 38);            game.load.image('arrow', 'images/arrow.png');            game.load.image('heart', 'images/heart.png');            game.load.spritesheet('coin', 'images/coin-sprite-animation-sprite-sheet.png', 44, 40);        }        function create () {              game.physics.startSystem(Phaser.Physics.ARCADE);            //Tilemap            map = game.add.tilemap('myMap');            map.addTilesetImage('scifi_platformTiles_32x32', 'sheet');            backgroundLayer = map.createLayer('Background');            blocks = map.createLayer('Blocks');            map.setCollision(317, true,  blocks);            backgroundLayer.resizeWorld();                //Heart            game.add.sprite(20, 20, 'heart');                     //Coin            coin = game.add.sprite(Math.floor((Math.random() * game.world.width) + 1),Math.floor((Math.random() * game.world.height) + 1), 'coin');                          coin.scale.setTo(.5,.5);            game.physics.arcade.enable(coin);               coin.enableBody = true;              coin.body.collideWorldBounds = true;            //Arrow            arrows = game.add.group();            arrows.enableBody = true;            arrows.physicsBodyType = Phaser.Physics.ARCADE;            arrows.createMultiple(40,'arrow');            arrows.setAll('checkWorldBounds', true);            arrows.setAll('outOfBoundsKill', true);                 //  Player            player = game.add.sprite(32, game.world.height - 150, 'dude');               player.scale.setTo(1.2,1.2);            game.physics.arcade.enable(player);               player.body.bounce.y = 0.1;            player.body.gravity.y = 300;            player.body.collideWorldBounds = true;                     //Animations            player.animations.add('left', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 12, true);            player.animations.add('right', [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], 12, true);            player.animations.add('jump_left', [26], 12, true);            player.animations.add('jump_right', [25], 12, true);            coin.animations.add('spin', [0,1,2,3,4,5,6,7,8,9], 24, true);            //Text Elements            score_text = game.add.text(0, 0, "Score\n" + '0', { font: "18px Arial", fill: "#ffffff", align: "center", boundsAlignH: "center", boundsAlignV: "middle"  });            score_text.setTextBounds(0, 0, 640, 80);            arrows_text = game.add.text(540, 20, "Arrows: " + (remaining_arrows), { font: "18px Arial", fill: "#ffffff" });            //Keyboard Elements            cursor = game.input.keyboard.createCursorKeys();            game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]);        }        function update() {                           game.physics.arcade.collide(player, blocks);              game.physics.arcade.collide(arrows, blocks, stopArrow, null, this);                 game.physics.arcade.collide(coin, blocks, replaceCoin, null, this);                          game.physics.arcade.collide(arrows, coin, shootCoin, null, this);                         game.physics.arcade.overlap(player, coin, collectCoin, null, this);                 coin.visible = true;            coin.animations.play('spin');                player.body.velocity.x = 0;                     if (cursor.left.isDown){                //  Move to the left                player.body.velocity.x = -150;                     if(!player.body.blocked.down){                    player.animations.play('jump_left');                }else{                       player.animations.play('left');                }                last_clicked = 'left';            }else if (cursor.right.isDown){                //  Move to the right                player.body.velocity.x = 150;                     if(!player.body.blocked.down){                    player.animations.play('jump_right');                }else{                    player.animations.play('right');                }                  last_clicked = 'right';                            }else{                //  Stand still                player.animations.stop();                         player.frame = 12;            }                        //  Allow the player to jump if they are touching the ground.            if (cursor.up.isDown && player.body.blocked.down) {                if(last_clicked == 'left'){                    player.animations.play('jump_left');                }else if(last_clicked == 'right'){                    player.animations.play('jump_right');                }                player.body.velocity.y = -350;            }            //  Fire Arrow            if (game.input.activePointer.isDown){                fireArrow();            }        }        function fireArrow(){            //Fire Arrows            if(game.time.now > arrowTime){                arrow = arrows.getFirstExists(false);                if(arrow){                    arrow.reset(player.body.x + 16, player.body.y + 16);                    arrow.rotation = game.physics.arcade.angleToPointer(arrow);                    game.physics.arcade.moveToPointer(arrow, 300);                    arrowTime = game.time.now + 100;                    new_remaining_arrows = remaining_arrows - arrows.total;                    arrows_text.setText("Arrows: " + (new_remaining_arrows));                }            }        }        function collectCoin (player, coin) {            // Removes the coin from the screen            coin.kill();            arrows.createMultiple(10,'arrow');            remaining_arrows = new_remaining_arrows + 10;;            arrows_text.setText("Arrows: " + remaining_arrows);            score_counter = score_counter + 500;            score_text.setText("Score\n" + score_counter);            addCoin();        }        function shootCoin (coin, arrow) {            // Removes the coin from the screen            coin.kill();            arrows.createMultiple(5,'arrow');            remaining_arrows = new_remaining_arrows + 5;            arrows_text.setText("Arrows: " + remaining_arrows);            score_counter = score_counter + 100;            score_text.setText("Score\n" + score_counter);            addCoin();        }        function stopArrow (arrow) {            arrow.body.velocity = 0;          }        function addCoin(){            //Add coin            coin = game.add.sprite(Math.floor((Math.random() * game.world.width) + 1),Math.floor((Math.random() * game.world.height) + 1), 'coin');              coin.visible = false;            coin.scale.setTo(.5,.5);                              game.physics.arcade.enable(coin);                coin.enableBody = true;            coin.animations.add('spin', [0,1,2,3,4,5,6,7,8,9], 24, true);                 }        function replaceCoin(){            alert('Test Collision.');            //coin.kill();            //addCoin();          }    };

Share this post


Link to post
Share on other sites

I'm replying to myself in case anyone else runs into this issue.  Here is how I solved it.

 

I created a function to get random x,y coordinates and then ran them through the getTileWorldXY function to retrieve the tile index of the random coordinates. If the tile matched my floor tile, I just re-run the function.

function getRandomCoordinates(){            var randX = Math.floor((Math.random() * game.world.width) + 1);            var randY = Math.floor((Math.random() * game.world.height) + 1);            var tileId = map.getTileWorldXY(randX,randY,32,32,blocks,true).index;            if(tileId == 317){                getRandomCoordinates();            }else{                randomX = randX;                randomY = randY;                return;            }        }

Share this post


Link to post
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.

Guest
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.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.