Gå til innhold

Problemer med å finne feil i AS3


Anbefalte innlegg

Hei, driver på med eit skuleprosjekt og sliter med ein feilkode vi ikkje skjønner ka er. Vi har ein animasjon som er knytta opp til ein package og feilmeldinga er i packagen.

 

Sjølve koden:

stop();
var mainSpeed:int = 7;
var CHICKYTime:int = 0;
var CHICKYLimit:int = 16;
var score:int = 0;
var gameOver:Boolean = false;

//-------Stjernar i bakgrunnen-----------------------------------------------//

stage.addEventListener(Event.ENTER_FRAME, generateParticles);

if(particleContainer == null){
var particleContainer:MovieClip = new MovieClip();
addChild(particleContainer);
}

function generateParticles(event:Event):void{
if(Math.random()*10 < 2){
	var mcParticle:Shape = new Shape(); 
	var dimensions:int = int(Math.random()*2)+1;
	mcParticle.graphics.beginFill(0xffffff,1);
	mcParticle.graphics.drawRect(dimensions,dimensions,dimensions,dimensions);
	mcParticle.x = int(Math.random()*stage.stageWidth);
	mcParticle.y = int(Math.random()*stage.stageHeight);
	particleContainer.addChild(mcParticle);
}

for(var i:int=0;i<particleContainer.numChildren;i++){
	var theParticle:DisplayObject = particleContainer.getChildAt(i);
	theParticle.y += mainSpeed*.5;
	if(theParticle.y >= 800){
		particleContainer.removeChild(theParticle);
	}
}
}

//-------------------------Styre raketten-------------------------------------//
//These variables tell us which button is down
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var upArrow:Boolean = false;

//rotationSpeed defines how fast the ROCKY rotates
var rotationSpeed:Number = 15;

//accelaration defines how fast we accelerate the ship
var acceleration:Number = 0.25;

//Ship x and y speed
var xSpeed:Number = 0;
var ySpeed:Number = 0;

//Add ENTER_FRAME that moves the ship
addEventListener (Event.ENTER_FRAME,moveRocket);

//Listen for the key presses
stage.addEventListener (KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP,keyUpHandler);

function moveRocket (e:Event):void {

//First we rotate the ship
if(rightArrow) {
	ROCKY.rotation += rotationSpeed;
}
if(leftArrow) {
	ROCKY.rotation -= rotationSpeed;
}


//If the user presses the up arrow, the following gets executed
if(upArrow) {

	//Increase the x and y speeds
	xSpeed += Math.cos(Math.PI*ROCKY.rotation/180)*acceleration;
	ySpeed += Math.sin(Math.PI*ROCKY.rotation/180)*acceleration;

	//Create a fire when we acce
	var fire:MovieClip = new MovieClip();

	//Draw the fire
	drawFire(fire);

	//Create a new fire at the end of the ROCKY
	fire.x = ROCKY.x - Math.cos(Math.PI*ROCKY.rotation/180)
			* (ROCKY.width / 2);
	fire.y = ROCKY.y - Math.sin(Math.PI*ROCKY.rotation/180)
			* (ROCKY.height / 2);

	//Add the fire to the stage
	addChildAt(fire,0);

	fire.addEventListener(Event.ENTER_FRAME, animateFire);
}

//Assign the new x and y position for the ROCKY
ROCKY.x +=  xSpeed;
ROCKY.y +=  ySpeed;
var maxspeedG:Number = 9;
var maxspeed:Number = 6;

//Make the ROCKY appear on the other side if it's out of bounds
if (ROCKY.x > stage.stageWidth) {
	ROCKY.x = 0;
}
else if (ROCKY.x < 0) {
	ROCKY.x = stage.stageWidth;
}
if (ROCKY.y > stage.stageHeight) {
	ROCKY.y = 0;
}
if (ROCKY.y < 0) {
	ROCKY.y = stage.stageHeight;
}

}

//This is called when a user presses a key
function keyDownHandler (e:KeyboardEvent):void {

//Left arrow clicked
if (e.keyCode == 37) {
	leftArrow = true;
}

//Right arrow clicked
else if (e.keyCode == 39) {
	rightArrow = true;
}

//Up arrow clicked
else if (e.keyCode == 38) {
	upArrow = true;
}
}

//This is called when a user releases a key
function keyUpHandler (e:KeyboardEvent):void {

//Left arrow up
if (e.keyCode == 37) {
	leftArrow = false;
}

//Right arrow up
else if (e.keyCode == 39) {
	rightArrow = false;
}

//Up arrow up
else if (e.keyCode == 38) {
	upArrow = false;
}
}

//This funtion draws a red ball
function drawFire (mc:MovieClip):void {

//Give a random color for the circle
mc.graphics.beginFill (0xff0000);

//Draw the cirlce
mc.graphics.drawCircle (0, 0, 7);

//End the filling
mc.graphics.endFill ();
}

//This function fades the fire in each frame
function animateFire(e:Event):void {

//In each frame, reduce the alpha and the scale of the trail ball.
e.target.alpha -= 0.04;
e.target.scaleY -= 0.04;
e.target.scaleX -= 0.04;

//Remove the fire if the alpha is less than zero
if(e.target.alpha<0) {
	e.target.removeEventListener(Event.ENTER_FRAME, animateFire);
	removeChild((MovieClip)(e.target));
}
}
//-------------------------Ny fiende--------------------------------------------//

ROCKY.addEventListener(Event.ENTER_FRAME,newChicken);
function newChicken(event:Event):void{
if(CHICKYTime < CHICKYLimit){
	CHICKYTime ++;
} else {
	var newChicken = new CHICKY();
	newChicken.y = -1 * newChicken.height;
	newChicken.x = int(Math.random()*(stage.stageWidth - newChicken.width));
	addChild(newChicken);
	CHICKYTime = 0;
}
txtScore.text = ''+score;
}

 

og her er packagen:

 

package{
import flash.display.MovieClip;
import flash.events.*;
public class CHICKY extends MovieClip{
	private var _root:Object;
	private var speed:int = 5;
	public function CHICKY(){
		addEventListener(Event.ADDED, beginClass);
		addEventListener(Event.ENTER_FRAME, eFrame);
	}
	private function beginClass(event:Event):void{
		_root = MovieClip(root);
	}
	private function eFrame(event:Event):void{
		y += speed;
		if(this.y > stage.stageHeight){
			removeEventListener(Event.ENTER_FRAME, eFrame);
			_root.removeChild(this);
		}
		if(CHICKY.hitTestObject(_root.ROCKY)){
			_root.gameOver = true;
			_root.gotoAndStop(5);

		}

		if(_root.gameOver){
			removeEventListener(Event.ENTER_FRAME, eFrame);
			this.parent.removeChild(this);

		}
	}
	public function removeListeners():void{
		this.removeEventListener(Event.ENTER_FRAME, eFrame);
	}
}
}

 

og så feilmeldinga vi får:

 

TypeError: Error #2007: Parameter hitTestObject must be non-null.

at flash.display::DisplayObject/flash.display:DisplayObject::_hitTest()

at flash.display::DisplayObject/hitTestObject()

at CHICKY/::eFrame()

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at PeterVSEvilChicken_fla::MainTimeline/PeterVSEvilChicken_fla::frame5()

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at PeterVSEvilChicken_fla::MainTimeline/moveRocket()

 

Problemer er egentlig at vi ikkje skjønner ka som er feil, berre at det er noke med hitTestObjecten i packagen. Noken som kan hjelpe? :)

 

På forhånd takk :)

Lenke til kommentar
  • 6 måneder senere...
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...