Jump to content

Why does collision fail when I increase span time to less than 1000ms or increate gravity of an item?


ciscog
 Share

Recommended Posts

I'm working on my first game that's pretty close to done, I'm just tweaking it to make it just a little bit more challenging.  It's just a fun little thing I made as an extra show piece for a website my friend is building and to learn how to make a game.  Right now I have a random item spawning once a second, it gets added to a group, is set to kill on out of bounds, and has physics arcade enabled on it.  

 

Here are current issues:

  1. collision happens but halfway through the player sprite.  I think this might be related to the anchor point being in the middle of the item sprite but if I change that then I loose rotation on the center of the item
  2. I tried to implement a random spawn time between 500 - 1500ms but any of the items spawned under 1000ms just went right through my player sprite
  3. I tried randomizing the gravity.y of an item sprite upon spawning but again the faster ones just go straight through the player sprite

Is collision detection slow?

 

am I doing something wrong?  

 

I just want to add a simple amount more of challenge to the game.  

 

playable game:

http://www.franciscog.com/sk-game/

debug mode: http://www.franciscog.com/sk-game/?debug

 

click on game or press up to start

press M to mute music (only music is muted so far)

 

 

full game code here:

https://github.com/FranciscoG/sk-game/blob/master/game/game.js

 

here's my Play state create function: (skGame is the main object and holds some globals)

create: function () {    var h = skGame.h, w = skGame.w;    // add the background    game.add.sprite(0, 0, 'bg');    // initiate game physics    game.physics.startSystem(Phaser.Physics.ARCADE);    game.physics.arcade.gravity.y = 200;    game.physics.arcade.setBounds(0, 0, w, h - 29);    // setup score    skGame.score = 0;    this.scoreText = game.add.text(10, 10, "0", { font: '30px Arial', fill: '#fff'});    // inituate cursor key inputs    this.cursor = game.input.keyboard.createCursorKeys();    // create Group of items    //this.spawnItemTimer = 0;    this.itemGroup = game.add.group();    this.itemGroup.setAll('outOfBoundsKill', true);    this.spawnItem();    // add hearts to screen    this.health = 3;    this.heartGroup = game.add.group();    var heart,        i = this.health,        x = w - 40;    while (i > 0) {      heart = game.add.sprite(x, 10, 'heart');      this.heartGroup.add(heart);      x = x - 35;      i--;    }        // add the player to the screen    this.player = game.add.sprite(w/2, h - 30, 'player');    this.player.anchor.setTo(0.5,0);        // setup player    this.player.animations.add('run', [1,2,3,4,5,6,7,8], 15, true);    this.player.animations.add('hurt', [9,10], 15, true);    this.isHurt = false;    game.physics.arcade.enable(this.player);    this.player.body.collideWorldBounds = true;    // shrink the bounding box of the player because there's some transparent    // space that allows collisions    var heightOffset = 0;    this.player.body.setSize(60, 110 + heightOffset, 0, 0 - heightOffset);    //audio    this.sounds = {};    // sound FX    this.sounds.pickup = game.add.audio('pickup');    this.sounds.coin = game.add.audio('coin');    this.sounds.hurt = game.add.audio('hurt');    this.sounds.necklace = game.add.audio('necklace');    // create spawnItem loop    this.looping = game.time.events.loop(1000, this.spawnItem, this);    // init mute key    skGame.mute();        // show item paths in debug mode    this.showColumns();  },

here's my update function:  
 

update: function() {    var h = skGame.h, w = skGame.w, score = skGame.score;    // rotate the item    this.itemGroup.forEachAlive(function(item){        item.angle += item.rotateMe || 0;    });        this.player.body.velocity.x = 0;    // if player is hurt do this    if (this.isHurt) {      this.player.animations.play('hurt');      if (this.game.time.now - this.hurtTime >= 500) {        this.isHurt = false;      }    }    // when player is moving    if (!this.isHurt){      if (this.cursor.left.isDown) {        this.player.body.velocity.x = -350;        this.player.animations.play('run');        this.player.scale.x = 1;      } else if (this.cursor.right.isDown) {         this.player.body.velocity.x = 350;         this.player.scale.x = -1;         this.player.animations.play('run');      } else {        this.player.frame = 0;      }    }  },

and here's my Spawn Item function:

spawnItem: function() {    var h = skGame.h, w = skGame.w;    // I want the items to fall in specific columns on the screen so first I need to    // figure out how many columns available that are the width of the sprite    var spriteW = 60;    var sp = Math.floor(w / spriteW); // calc number of columns in stage    var dropPos = rand(sp); // get random column    // now place it on screen by adding it to a group    var itemX = dropPos * spriteW + (spriteW/2);    var item = game.add.sprite(itemX, 0, 'items', null, this.itemGroup);    // get a random Item from the spritesheet. 11 items total    var itemType = rand(11);    if (itemType === 10) {      item.animations.add('blink', [17,18], 10, true);      item.animations.play('blink');      item.itemName = "necklace";    } else if (itemType === 9) {      item.animations.add('coin_twirl', [9,10,11,12,13,14,15,16], 10, true);      item.animations.play('coin_twirl');      item.itemName = "coin";    } else if (itemType <= 5 ) {      item.itemName = "normal_item";      item.frame = itemType;    } else { // items 6,7,8      item.itemName = "bad_item";      item.frame = itemType;    }    item.rotateMe = (Math.random()*4)-2;    // enable physics    game.physics.enable(item, Phaser.Physics.ARCADE);        // shrink the bounding box of the item because there's some transparent area    item.body.setSize(55, 60, 0, 0);    item.anchor.setTo(0.5, 0.5);    game.physics.arcade.overlap(this.player, this.itemGroup, this.grabItem, null, this);  },

thanks!

Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...