다리 작업을 완료하고 문제를 추적했습니다. 전화 할 때as3 - 오류 # 2007 : 매개 변수 hitTestObject가 null이 아니어야하며 오류가 발생했습니다. 솔루션이 필요합니다.
if (enemy.hitTestObject(bullet)){
글 머리 기호가 null입니다.
나는
var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation);
함수의 내부에서 생성되는 대신 클래스의 속성 public var로 작성되고 있기 때문에 문제가 발생합니다 생각합니다. 그러나이 코드 줄을 다른 공개 바 (public vars)로 옮기고 (공개로 변경하면) 총알 촬영이 중단됩니다.
그렇다면 질문은 어떻게 총알을 null이 아닌 값으로 만들 수 있습니까?
Main.as :
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
public class zombiestandoffMain extends MovieClip
{
public var army:Array;
public var enemy:Enemy;
public var player:Player;
public var bullet:Bullet;
public var gameTimer:Timer;
public var crosshair:Crosshair;
public var bulletList:Array = []; //new array for the bullets
public function zombiestandoffMain()
{
army = new Array();
var newEnemy = new Enemy(100, -15);
army.push(newEnemy);
addChild(newEnemy);
player = new Player(stage, 400, 500);
stage.addChild(player);
crosshair = new Crosshair();
addChild(crosshair);
crosshair.x = mouseX;
crosshair.y = mouseY;
gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true); //add an EventListener for the loop
function loop(e:Event):void //create the loop function
{
if(bulletList.length > 0) //if there are any bullets in the bullet list
{
for(var i:int = bulletList.length-1; i >= 0; i--) //for each one
{
bulletList[i].loop(); //call its loop() function
}
}
}
function shootBullet(e:MouseEvent):void
{
var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation);
bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); //triggers the "bulletRemoved()" function whenever this bullet is removed from the stage
bulletList.push(bullet); //add this bullet to the bulletList array
stage.addChild(bullet);
}
function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved); //remove the event listener so we don't get any errors
bulletList.splice(bulletList.indexOf(e.currentTarget),1); //remove this bullet from the bulletList array
}
}
public function onTick(timerEvent:TimerEvent):void
{
if (Math.random() < 0.1)
{
var randomX:Number = Math.random() * 400;
var newEnemy:Enemy = new Enemy(randomX, -15);
army.push(newEnemy);
addChild(newEnemy);
}
crosshair.x = mouseX;
crosshair.y = mouseY;
for each (var enemy:Enemy in army)
{
enemy.moveDownABit();
trace(enemy);
trace(bullet);
if (enemy.hitTestObject(bullet)){
trace("hit!");
}
}
}
}
}
Bullet.as는
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip
{
private var stageRef:Stage;
private var speed:Number = 10;
private var xVel:Number = 0;
private var yVel:Number = 0;
private var rotationInRadians = 0;
public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
this.rotation = rotationInDegrees;
this.rotationInRadians = rotationInDegrees * Math.PI/180;
}
public function loop():void
{
xVel = Math.cos(rotationInRadians) * speed;
yVel = Math.sin(rotationInRadians) * speed;
x += xVel;
y += yVel;
if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
{
this.parent.removeChild(this);
}
}
}
}
어떤 도움 주셔서 감사합니다!
의
이 어떻게 총알 촬영을 깰 않습니다 쓰기 시도? 그것은 창조, 움직임, 적 타격 또는 다른 것을 깨뜨릴 수 있습니까? – puggsoy
그것은 내가 재검사 한 지금 실제로 전체 게임을 깬다. 내가 공용 var로 이동하면 "오류 # 1009 : 속성 또는 null 객체 참조의 메소드에 액세스 할 수 없습니다."오류가 발생합니다. 해당 코드 행을 참조하십시오. – jdfinch3