Im working on shooting some bullets and the collision testing works perfectly fine it even hurts the zombies. Except the one problem, i want the bullets to disappear when they hit a zombie this sounds easy sense Im already hit testing inside the "bulletContainer" Class, well long story short i call the bulletContain[a].killBullet(); and output dialouge says its not a function(I know for a fact its a function and the "bulletClass" Class it self is loading properly). It seems once i create the bullet to shoot it has a mind of its own and doesn't listen to any variables i change inside it for me to tell it needs to disappear/killBullet()
Heres Snippets of my code please comment if you feel like you need more(althought there isnt much)
BulletContainer Class(Showing Just testCollosion Function):
//Collision Tester/////////////////////////////////////////////
private function testCollision(){
//Get number of bullets on screen
var numBullets = bulletContain.length;
//Define a local variable for ease of reference
var localZombieContain = Object(root).zombieContain.zombieContain
var numZombies:Number = localZombieContain.length;
//Loop through every bullet for collision test
for(var a:Number = 0; a < numBullets; a++){
//Loop through every zombie & hit test to see if bullet is hitting
for(var b:Number = 0; b < numZombies; b++){
var killBullet:Boolean = false;
if(localZombieContain[b].hitTestObject(bulletContain[a])){
//Tell zombie it has been hit
localZombieContain[b].hurtZombie();
//Tell Bullet its dead for garbage
killBullet = true;
}
if(killBullet){
bulletContain[a].killBullet();
trace("BULLET DIED");
}
}
}
BulletClass(Showing everything from bulletClass): (IGNORE REDENDENT SWITCH() CODES, THATS FOR LATER DEVELOPMENT USE)
package com{
import flash.display.*
import flash.net.*
import flash.utils.*
import flash.events.*
public class bulletClass extends MovieClip{
public var dead = 0;
var moveBulletInt;
public function bulletClass(type:Number) {
//Declare bullet types from library
var tmpBullet1 = new bullet1;
switch(type){开发者_如何学Python
case 1:
addChild(tmpBullet1);
break;
}
//Move this bullet
moveBulletInt = setInterval(moveBullet, 10);
}
public function killBullet(){
clearInterval(moveBulletInt);
trace("BULLET DIED");
}
private function moveBullet(){
if(dead == 0){
this.x += 15 * Math.sin((this.rotation - 90) * (Math.PI / 180));
this.y += 15 * Math.cos((this.rotation - 90) * (Math.PI / 180));
}
if(dead == 1){
//clearInterval(moveBulletInt);
this.x = -100
this.y = -100
}
}
}
}
I was calling the movie clip instead of calling the class when i previously added bullets else where in my code. So i ended up just doing var newObject = newBullet; instead of the lather newObject.addChild(newBullet);
精彩评论