0

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);
}
caitriona
  • 8,569
  • 4
  • 32
  • 36

4 Answers4

2

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.

0

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.

SvEnjoyPro
  • 764
  • 1
  • 9
  • 12
  • Hi, thanks for the reply but its already placed at the stage(dragged the movieclip from library to the stage if this is what) and in its linkage settings its actionscript name is mcEnergy and its being exported to first frame also, so just changed its movieclip name from mcEnergy to mc_Energy and still no effect uploaded the files can you have a look??? http://www.filefactory.com/file/30t0opvs2lrb/n/Shooting_game_fla http://www.filefactory.com/file/tigcdflf1sd/n/Shooting_game_swf oh and the cursor and monsters arent being made either, forgot to mention that. – Taimoor Abdullah Khan Aug 12 '12 at 06:36
  • I was surprised that it was not the problem so, I had a look and it's exactly what I said... Here's the .fla file : http://tatactic.be/downloads/download.php?fid=as3Reply_045 You don't even have to link it for AS3 since it's an instance named on stage that you want to retrieve and not an item in the library... – tatactic Aug 13 '12 at 20:08
0

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 {...}

tatactic
  • 1,379
  • 1
  • 9
  • 18
0

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 );
Raohmaru
  • 140
  • 5