问题描述:

learning action script 3.0 from "http://www.lynda.com/ActionScript-3-tutorials/projects-game-development/366-2.html" and on its "chapter 1> winning a game" where score and damage to us is defined, i am getting errors of my movie clip.

Using three movie clips which i had to make and didn't download from their files: one is "cursor", one "monster" for which there is coding (look at bottom) and third is "mcEnergy" which is causing the problem (is a static class and shouldn't be or something (really new to flash programming)), it has three frames to show health at three stages.

so tell me how to fix this static class error, gona post the codes and errors below anyway if it helps.

Thanks in advance.

the three errors are

 1119: Access of possibly undefined property totalFrames through a reference with static type Class.

Source: energy = mcEnergy.totalFrames;

1061: Call to a possibly undefined method gotoAndStop through a reference with static type Class.

mcEnergy.gotoAndStop(energy);

1061: Call to a possibly undefined method gotoAndStop through a reference with static type Class.

mcEnergy.gotoAndStop(energy);

Here is the main stage code

var monstersInGame:uint;

var monsterMaker:Timer;

var container_mc:MovieClip;

var cursor:MovieClip;

var score:int;

var energy:int;

function initializeGame():void

{

monstersInGame = 10;

monsterMaker = new Timer(1000, monstersInGame);

container_mc = new MovieClip();

addChild(container_mc);

monsterMaker.addEventListener(TimerEvent.TIMER, createMonsters);

monsterMaker.start();

cursor = new Cursor();

addChild(cursor);

cursor.enabled = false;

Mouse.hide();

stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);

score = 0;

energy = mcEnergy.totalFrames;

mcEnergy.gotoAndStop(energy);

}

function dragCursor(event:MouseEvent):void

{

cursor.x = this.mouseX;

cursor.y = this.mouseY;

}

function createMonsters(event:TimerEvent):void

{

var monster:MovieClip

monster = new Monster();

monster.x = Math.random() * stage.stageWidth;

monster.y = Math.random() * stage.stageHeight;

container_mc.addChild(monster);

}

function increaseScore():void

{

score ++;

if(score >= monstersInGame);

{

monsterMaker.stop();

trace("Winning!!!");

}

}

function decreaseEnergy():void

{

energy --;

if(energy <= 0)

{

monsterMaker.stop();

trace("You lose");

}

else

{

mcEnergy.gotoAndStop(energy);

}

}

initializeGame();

and here is for the monsters

import fl.motion.Animator;

import fl.motion.MotionEvent;

var this_xml:XML = <Motion duration="30" xmlns="fl.motion.*" xmlns:geom="flash.geom.*" xmlns:filters="flash.filters.*">

<source>

<Source frameRate="12" x="227.65" y="291.3" scaleX="1" scaleY="1" rotation="0" elementType="movie clip" symbolName="Monster">

<dimensions>

<geom:Rectangle left="-17.85" top="-17.85" width="35.7" height="35.7"/>

</dimensions>

<transformationPoint>

<geom:Point x="0.5" y="0.5"/>

</transformationPoint>

</Source>

</source>

<Keyframe index="0" tweenSnap="true" tweenSync="true">

<tweens>

<SimpleEase ease="0"/>

</tweens>

</Keyframe>

<Keyframe index="29" scaleX="2.357" scaleY="2.357"/>

</Motion>;

var this_animator:Animator = new Animator(this_xml, this);

this_animator.play();

this_animator.addEventListener(MotionEvent.MOTION_END, hurtPlayer);

function hurtPlayer(Event:MotionEvent):void

{

var main:MovieClip = MovieClip(this.parent.parent);

main.decreaseEnergy();

this.parent.removeChild(this);

}

this.addEventListener(MouseEvent.CLICK, killMonster);

function killMonster(event:MouseEvent):void

{

this_xml = <Motion duration="5" xmlns="fl.motion.*" xmlns:geom="flash.geom.*" xmlns:filters="flash.filters.*">

<source>

<Source frameRate="12" x="236.95" y="163" scaleX="1" scaleY="1" rotation="0" elementType="movie clip" symbolName="Monster" class="Monster">

<dimensions>

<geom:Rectangle left="-17.85" top="-17.85" width="35.7" height="35.7"/>

</dimensions>

<transformationPoint>

<geom:Point x="0.5" y="0.5"/>

</transformationPoint>

</Source>

</source>

<Keyframe index="0" tweenSnap="true" tweenSync="true">

<tweens>

<SimpleEase ease="0"/>

</tweens>

</Keyframe>

<Keyframe index="4" scaleX="0.552" scaleY="0.552">

<color>

<Color alphaMultiplier="0"/>

</color>

</Keyframe>

</Motion>;

this_animator = new Animator(this_xml, this);

this_animator.play();

this_animator.addEventListener(MotionEvent.MOTION_END, die);

}

function die(event:MotionEvent):void

{

var main:MovieClip = MovieClip(this.parent.parent);

main.increaseScore();

this_animator.removeEventListener(MotionEvent.MOTION_END, hurtPlayer);

this.parent.removeChild(this);

}

网友答案:

Okay, this is what's up. You're saying gotoAndStop(); but there is no gotoandstop function in your class, static class can only be called through a function, if you don't want it to be static then NEVER say myclass.somefunction(); instead call it from inside the class.

网友答案:

The issue is that you have a class named mcEnergy. You need an instance of a class with the instance name of mcEnergy. Do you have an instance of your mcEnergy class on the stage? If so, what is it's instance name? If not, you need to make one and put it there; whether you do that at build time (drag and drop one from the library onto the stage and set it's instance name to something other than mcEnergy) or whether you create it via actionscript at run time it doesn't matter, but that's the variable you need to use there.

A heads up, though, it's considered best practice to capitalize the first letter of class names. So you have a movieclip in your library currently named mcEnergy, it should be McEnergy. You can then make your instance's name mcEnergy and the code you currently have should work.

网友答案:

Your instance is probably not named on stage in the properties but just exported for AS if I understand the problem. Try to replace every reference to mcEnergy on the Stage by :

MovieClip(getChildAt(0));

If this solve the problem , you should only declare your instance as here-bellow in your main class :

var mcEnergy:MovieClip = MovieClip(getChildAt(0));

But you'd better name the instance "mcEnergy" in the properties and do :

var mcEnergy:MovieClip = MovieClip(getChildByName("mcEnergy"));

You have a ";" in

 if(score >= monstersInGame);

Your code will not execute the statements in {...}

网友答案:

As answered by tatactic, it seems that mcEnergy is the class name of the symbol at the library, and not the name of an instance in the stage. You need to create a new instance of mcEnergy and then add it to the display list.

var energy_mc :MovieClip = new mcEnergy();
stage.addChild( energy_mc );

After that, replace the references to mcEnergy by energy_mc:

energy = energy_mc.totalFrames;
energy_mc.gotoAndStop( energy );
相关阅读:
Top