PDA

View Full Version : [AS3] How do I Add a Energy Bar to my Shooter Game!


scott1969
04-09-2008, 08:25 PM
I am very new to AS3..

I am creating a kids game in AS3. There is a smile meter that goes up when you click on a good character and down when you click on a bad character. If you reach the top you win and if you reach the bottom you you lose.

What is the best way to achieve this? Attached is my Stage source code.
Any help would be greatly appreciated.

Thanks,
Scott

I asked the same question in the kirupa forum, but I cant seem to piece the code together correctly. Can anyone help? Here are my codes:


AS3 CODE ON FRAME 1:

var duckTimer:Timer = new Timer(2000, 1);
duckTimer.addEventListener(TimerEvent.TIMER, makeADuck);
duckTimer.start();


function makeADuck(evt:TimerEvent):void {

var newDuck:duck = new Duck();
var duckIndex:Number = this.getChildIndex(gdCheet1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 204.8;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);
}


var duckTimer2:Timer = new Timer(8000,1);
duckTimer2.addEventListener(TimerEvent.TIMER, makeADuck2);
duckTimer2.start();

function makeADuck2(evt:TimerEvent):void {

var newDuck:duck2 = new Duck2();
var duckIndex:Number = this.getChildIndex(duck2Fnl)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 178.6;
newDuck.swim();

}

var duckTimer3:Timer = new Timer(10000,1);
duckTimer3.addEventListener(TimerEvent.TIMER, makeADuck3);
duckTimer3.start();

function makeADuck3(evt:TimerEvent):void {

var newDuck:duck3 = new Duck3();
var duckIndex:Number = this.getChildIndex(bamboo1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 100.0;
newDuck.swim();


}

var duckTimer4:Timer = new Timer(15000,1);
duckTimer4.addEventListener(TimerEvent.TIMER, makeADuck4);
duckTimer4.start();

function makeADuck4(evt:TimerEvent):void {

var newDuck:Duck4 = new Duck4();
var duckIndex:Number = this.getChildIndex(goodMnky2)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 147.6;
newDuck.swim();

}

var duckTimer5:Timer = new Timer(15000,1);
duckTimer5.addEventListener(TimerEvent.TIMER, makeADuck5);
duckTimer5.start();

function makeADuck5(evt:TimerEvent):void {

var newDuck:duck5 = new Duck5();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 58.1;
newDuck.swim();

}

var duckTimer6:Timer = new Timer(25000,1);
duckTimer6.addEventListener(TimerEvent.TIMER, makeADuck6);
duckTimer6.start();

function makeADuck6(evt:TimerEvent):void {

var newDuck:duck6 = new Duck6();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 389.6;
newDuck.swim();

}



var duckTimer7:Timer = new Timer(5000,1);
duckTimer7.addEventListener(TimerEvent.TIMER, makeADuck7);
duckTimer7.start();

function makeADuck7(evt:TimerEvent):void {

var newDuck:duck7 = new Duck7();
var duckIndex:Number = this.getChildIndex(bamboo1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 374.6;
newDuck.swim();

}

var duckTimer8:Timer = new Timer(20000,1);
duckTimer8.addEventListener(TimerEvent.TIMER, makeADuck8);
duckTimer8.start();

function makeADuck8(evt:TimerEvent):void {

var newDuck:Duck8 = new Duck8();
var duckIndex:Number = this.getChildIndex(bamboo1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 485.5;
newDuck.swim();

}

var duckTimer9:Timer = new Timer(2500,1);
duckTimer9.addEventListener(TimerEvent.TIMER, makeADuck9);
duckTimer9.start();

function makeADuck9(evt:TimerEvent):void {

var newDuck:Duck9 = new Duck9();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 385.4;
newDuck.swim();

}

var duckTimer10:Timer = new Timer(7000,1);
duckTimer10.addEventListener(TimerEvent.TIMER, makeADuck10);
duckTimer10.start();

function makeADuck10(evt:TimerEvent):void {

var newDuck:Duck10 = new Duck10();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 352.3;
newDuck.swim();

}

function clickDuck(evt:MouseEvent):void {
evt.target.hit();
}

Mouse.hide();
mTarget.mouseEnabled = false;
function moveTarget(evt:MouseEvent):void {
//target graphic = mTarget
mTarget.x = evt.stageX;
mTarget.y = evt.stageY;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveT arget);




AS3 EXTERNAL FILES



package {

import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.motion.Animator;

public class Duck extends MovieClip {
var duckHit_animator:Animator;


function Duck():void {

}
public function swim():void {
var swimTween = new Tween(this, "x",None.easeNone,4.8,4.8, 5, true);

}
public function hit():void {
var duckHit_xml:XML = <Motion duration="10" xmlns="fl.motion.*" xmlns:geom="flash.geom.*" xmlns:filters="flash.filters.*">
<source>
<Source frameRate="12" x="-167.85" y="292.75" scaleX="1" scaleY="1" rotation="0" elementType="movie clip" symbolName="duckHit">
<dimensions>
<geom:Rectangle left="-85.65" top="-51" width="171.3" height="102"/>
</dimensions>
<transformationPoint>
<geom:Point x="0.5046701692936368" y="0.5"/>
</transformationPoint>
</Source>
</source>

<Keyframe index="0" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="4" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="45" blurY="45" color="0x3366FF" alpha="1" strength="1" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="9">
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>
</Motion>;

duckHit_animator = new Animator(duckHit_xml, this);
duckHit_animator.play();


}
}
}





SECOND AS3 FILE ect..

package{

import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;

public class Duck2 extends MovieClip {

function Duck2():void{

}
public function swim():void {
var swimTween = new Tween(this, "x",None.easeNone,302.4, 222, 4, true);

}

}
}

rrh
04-10-2008, 12:45 AM
For the sake of readability, surround code with [ as ] tags. And what was wrong with the advice you got on Kirupa?

http://www.kirupa.com/forum/showthread.php?t=294420

scott1969
04-10-2008, 02:56 PM
Nothing was wrong with the advice, it is my ability to apply it. I am not sure where to incorporate the code in my frame work. I just started learn AS3 and I am pretty much lost. I know I can figure it out, but time is of the essence..

Peace,
Scott

rrh
04-11-2008, 06:17 PM
I'm asking because they say basically the same thing I would say.

My standard question: What is it doing instead of working?

Maybe something they overlooked, if you access disposition from outside of the Duck class, it should be public:
public var disposition:String;

tkalim
02-24-2009, 09:52 PM
Hey there, I found a tutorial about this at freshnova. It helped me out, hope it helps you as well. http://www.freshnova.com/Flash-Tutorials/simpleenergybarinaflashgame.html. :)