所以我在我的一个敌人类中有这个代码用于我的flash游戏。
package
{
import flash.events.*;
import flash.display.*;
import flash.geom.*;
public class JumpBall extends MovieClip
{
var MTL:MovieClip = MovieClip(root);
var charMTL:MovieClip;
var bullet:Bullet = new Bullet;
public function JumpBall()
{
this.addEventListener(Event.ENTER_FRAME, EnemyBallUpdate);
}
function EnemyBallUpdate(event:Event):void
{
charMTL = MTL.char1;
var enemy:Rectangle = this.getBounds(this);
var enemy_matrix:Matrix = this.transform.matrix;
var enemyBitmap:BitmapData = new BitmapData(enemy.width, enemy.height, true, 0);
enemyBitmap.draw(this,enemy_matrix);
enemy_matrix.tx = this.x - enemy.x;
enemy_matrix.ty = this.y - enemy.y;
var char:Rectangle = charMTL.getBounds(this);
var char_matrix:Matrix = charMTL.transform.matrix;
var charBitmap:BitmapData = new BitmapData(char.width, char.height, true, 0);
charBitmap.draw(charMTL,char_matrix);
char_matrix.tx = charMTL.x - char.x;
char_matrix.ty = charMTL.y - char.y;
var enemyPoint:Point = new Point(enemy.x, enemy.y);
var charPoint:Point = new Point(char.x, char.y);
if(enemyBitmap.hitTest(enemyPoint, 0, charBitmap, charPoint, 0) && MTL.currentFrame == 2)
{
if(MTL.HP == 0)
{
MTL.RemoveListeners();
MTL.vcam_2.x = 400.2;
MTL.vcam_2.y = 224.9;
MTL.gotoAndStop(3);
MTL.HP = 3;
MTL.CC = 0;
MTL.removeChild(charMTL);
}else{
MTL.removeKeyboardEvts();
MTL.KeysOFF();
MTL.fade_mc.gotoAndPlay(2);
charMTL.gotoAndStop(1);
MTL.ySpeed = 0;
MTL.removeChild(charMTL);
}
}
enemyBitmap.dispose();
charBitmap.dispose();
}
public function removeJumpBallEL(event:Event):void
{
this.removeEventListener(Event.ENTER_FRAME, EnemyBallUpdate);
}
}
}
进入第2帧后,我收到了一个愚蠢的错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at JumpBall/EnemyBallUpdate()
我无法找出为什么班级不能与char1 MovieClip(游戏中的角色)对话。
我有一个类型的重置框类,但该类使用.hitTestPoint
方法。没有错误。
我试图将我的敌人添加到数组中以与子弹类进行交互。
这是主时间轴代码:
function addJumpBall():void
{
var jumpball:JumpBall = new JumpBall;
jumpball.x = 673;
jumpball.y = 318;
addChild(jumpball);
jumpball.addEventListener(Event.REMOVED, jumpballRemoved);
JumpBallArray.push(jumpball);
}
function jumpballRemoved(event:Event):void
{
event.currentTarget.removeEventListener(Event.REMOVED, jumpballRemoved);
JumpBallArray.splice(JumpBallArray.indexOf(event.currentTarget), 1);
}
addJumpBall();
感谢任何愿意帮助我的人!