Author Topic: [SOLVED] Game crashes on Bullet-Alien Overlap  (Read 884 times)

TVarmy

  • Member
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
[SOLVED] Game crashes on Bullet-Alien Overlap
« on: Fri, Sep 17, 2010 »
I'm just starting to learn actionscript and Flixel, as I've been wanting to start game design as a hobby.  I know my code is a mess and probably doesn't follow all the ideal practices, as I only have a Java computer science course under my belt.  It's a space shooter.

Also, it's half-copied from a tutorial, which I've been trying to embellish with the Japanese style vertical scrolling and a health bar, which is based on a snippet from FlashGameDojo.  I'm planning on adding healing items, new enemies, enemies that shoot back, bosses, levels and/or health regeneration as I get more competent.  This is for my personal use and I'm new to programming games, so please don't accuse me of being uncreative or borrowing too much code.  I'll write much more code on my own once I develop the right habbits.  Right now, I'm lucky if I remember to end a line with a semicolon.  :-\

Unfortunately, one of the most basic mechanics is making the game crash!  Every time I shoot an enemy (friendly bullet collides with alien), the game crashes and throws an error in FlashDevelop that says "[Fault] exception, information=TypeError: Error #1034: Type Coercion failed: cannot convert Bullet@53cfd41 to Alien."  It also points to line 422 in FlxQuadTree.as.

I really don't get what's going on, but here's my code for PlayState.as (the main FlxState).
Code: [Select]
package  
{
import org.flixel.*;
public class PlayState extends FlxState
{
private var _ship: Ship; //Blah blah blah declarations
private var _aliens: FlxGroup;
private var _friendlyBullets: FlxGroup;
private var _enemyBullets: FlxGroup;
private var _scoreText: FlxText;
private var _gameOverText: FlxText;
private var _spawnTimer: Number;
private var _spawnInterval: Number = 2.5;
private var sf:StarField = new StarField();
private var bar: FlxSprite;

override public function create():void
{
FlxG.score = 0; //Neutralizes score
add(sf); //Adds in that nifty starfield effect!  WARP 20!

_ship = new Ship(); //Adds the ship
add(_ship);

_aliens = new FlxGroup(); //adds the alien group
add(_aliens);

_friendlyBullets = new FlxGroup(); //freindly bullets
add(_friendlyBullets);

_enemyBullets = new FlxGroup(); //enemy bullets
add(_enemyBullets);

_scoreText = new FlxText(10, 8, 200, "0");
_scoreText.setFormat(null, 16, 0xFF597137, "left");
add(_scoreText);

resetSpawnTimer();
//Healthbar here
var frame:FlxSprite = new FlxSprite(30,10); //Frame for healthbar
frame.createGraphic(152,16); //White frame for the health bar
frame.scrollFactor.x = frame.scrollFactor.y = 0;
add(frame);

var inside:FlxSprite = new FlxSprite(31,11);
inside.createGraphic(150,14,0xff000000); //Black interior, 48 pixels wide
inside.scrollFactor.x = inside.scrollFactor.y = 0;
add(inside);

bar = new FlxSprite(31,11); //Creates healthbar
bar.createGraphic(1,14,0xffff0000); //The red bar itself
bar.scrollFactor.x = bar.scrollFactor.y = 0;
bar.origin.x = bar.origin.y = 0; //Zero out the origin
bar.scale.x = 150; //Fill up the health bar all the way
add(bar);
//End healthbar

super.create();
}

override public function update():void
{
FlxU.overlap(_aliens, _ship, overlapAlienShip);
FlxU.overlap(_friendlyBullets, _aliens, overlapAlienBullet);
if (FlxG.keys.justPressed("SPACE") && _ship.dead == false)
{
spawnBullet(_ship.getBulletSpawnPosition());
}

_spawnTimer -= FlxG.elapsed;

if(_spawnTimer < 0)
{
spawnAlien();
resetSpawnTimer();
}

if(FlxG.keys.ENTER && _ship.dead)
{
FlxG.state = new PlayState();
}


super.update();
if (_ship.health >= 1)
{
bar.scale.x = _ship.health * (150 / _ship.maxHealth); //Drop or raise the health bar proportional to ship health
}
else
{
_ship.kill();
bar.scale.x = 0;
FlxG.quake.start(0.05);
_ship.dead = true;
}
}
private function spawnAlien():void
{
var y: Number = -50;
var x: Number = Math.random() * (FlxG.width);
_aliens.add(new Alien(x, y));
}

private function spawnBullet(p: FlxPoint):void
{
var bullet: Bullet = new Bullet(p.x, p.y);
_friendlyBullets.add(bullet);
}

private function resetSpawnTimer():void
{
_spawnTimer = _spawnInterval;
_spawnInterval *= 0.95;
if(_spawnInterval < 0.1)
{
_spawnInterval = 0.1;
}
}
private function overlapAlienShip(alien:Alien, ship:Ship):void
{
ship.flicker(1);
alien.kill();
FlxG.quake.start(0.01);
_ship.health = _ship.health - 5;
bar.flicker(1);

}

private function overlapAlienBullet(alien:Alien, bullet:Bullet):void
{
alien.kill();
bullet.kill();
FlxG.score += 1;
_scoreText.text = FlxG.score.toString();
}

}

}

I assume the trouble happens when the game goes to the function overlapAlienBullet.  Other than that, since the crash occurs in Flixel, I'm clueless. ???

If I did anything wrong in this post, please forgive me.  It's my first one, and I really don't want to do anything wrong by this community, as flixel seems like a great framework, and I really appreciate any support I get in learning it!
« Last Edit: Fri, Sep 17, 2010 by TVarmy »

TVarmy

  • Member
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: [First post] Game crashes on Bullet-Alien Overlap
« Reply #1 on: Fri, Sep 17, 2010 »
Nevermind, I figured it out on my own.  I mixed up Aliens and Bullets in the function.  Derp. ::)

Anyway, it works now.  Time to add more features, and maybe some sounds and explosions.

OmegaStorm

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • OmegaStorm Productions
Re: [SOLVED] Game crashes on Bullet-Alien Overlap
« Reply #2 on: Fri, Sep 17, 2010 »
I wrote up an extension to that tutorial awhile back. You can find it on my site at http://www.omegastormproductions.com/uber-flash/flixel-2-tutorial-extended if you want to get ideas.