Changing the value of scaleX on a health bar when objects collide - actionscript-3

I have been trying loads of different ways to make my health bar scaleX increase by 25% incrementally when it hits targets.
So far the closest I've come is with else if statements. The problem is actionscript runs through the else if statements and doesn't stop, it does all of the calculations and makes my health bar full every time I hit something. I understand why this is happening (the else if statements are becoming true as AS3 runs through them) but I don't know how to stop it.
Any help is appreciated
var health:Number = 0;
var fullHealth:Number = 100;
healthBar.scaleX = health / fullHealth;
//collision
if(dog_mc.hitTestObject(deadRat_mc)){
deadRat_mc.visible=false;
if (healthBar.scaleX==0){
healthBar.scaleX =+.25;
}
else if (healthBar.scaleX==.25){
healthBar.scaleX =+.50;
}
else if (healthBar.scaleX==.50){
healthBar.scaleX =+.75;
}
else if (healthBar.scaleX==.75){
healthBar.scaleX =+1;
}
}

Use math, I'm guessing you copied that code somewhere but you don't know how to use it, health is meant to control the level of the health bar:
var health:Number = 0;
var fullHealth:Number = 100;
healthBar.scaleX = health / fullHealth;
if(dog_mc.hitTestObject(deadRat_mc))
{
health += 25;
}
That's all you need.

Related

Snakes and Ladders AS3.. how can a movie clip climb a ladder?

I am having this problem in developing this snakes and ladders game and i am very much hoping that you guys can help me out. i already created the board and the avatar. only thing is i cant make the avatar move up the ladder, and move down with the snake. can somebody help me? i am very much desperate right now, and every help is appreciated, thank you guys!
EDIT:
here's the code that i have written so far here are some of the codes I have written so far..
stop();
var xCoord:Array = [141,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251];
var yCoord:Array = [675,670,670,670,670,670,670,670,670,670,670,602,602,602,602,602,602,602,602,602,602,534,534,534,534,534,534,534,534,534,534,466,466,466,466,466,466,466,466,466,466,399,399,399,399,399,399,399,399,399,399,331,331,331,331,331,331,331,331,331,331,262,262,262,262,262,262,262,262,262,262,195,195,195,195,195,195,195,195,195,195,127,127,127,127,127,127,127,127,127,127,60,60,60,60,60,60,60,60,60,60];
var arrSquares:Array = new Array(xCoord.length);
var spaceIndex:Number = 0;
var delay:Number = 400;
var tm:Timer = new Timer(delay);
tm.addEventListener(TimerEvent.TIMER, mover);
tm.addEventListener(TimerEvent.TIMER_COMPLETE, moveDone);
spinner.addEventListener(MouseEvent.CLICK, doSpin);
var total:Number =0;
function doSpin(mevt:MouseEvent):void {
var rn:Number = Math.round(5*Math.random()+1);
txtCount.text = String(rn);
total = total + rn;
txtTotal.text = String(total);
txtCount.visible = true;
spinner.removeEventListener(MouseEvent.CLICK, doSpin);
tm.reset();
tm.repeatCount = rn;
tm.start();
}
function mover(tevt:TimerEvent):void {
spaceIndex = (spaceIndex+1)%(xCoord.length);
chip.x = xCoord[spaceIndex];
chip.y = yCoord[spaceIndex];
}
function moveDone(tevt:TimerEvent):void {
spinner.addEventListener(MouseEvent.CLICK, doSpin);
txtCount.visible = false;
}
i dont know where to put the if statement of executing the motion tween attached to the chip(avatar)
Well here are my initial thoughts. I would create an animation with your character who you want to move up and down. To make it simple, make an animation for climbing, then one for falling. It's really easy to do, just use the tween option on the timeline after inserting your keyframes.
Looks like you've created your animations. Now, whether or not he slides up or falls down depends on where your snakes and ladders are. Only you know that, so at those particular spots on your board (for example, x = 200 and y = 200) we want the if statement to occur. But we only want it to occur after your avatar is done moving. Probably create a new function and add the following to the mover function
var checkX:Number = chip.x;
var checkY:Number = chip.y
checkLandingSpace(checkX,checkY);
Now let's make a function checkLandingSpace which will check if we are on a shoot or ladder
function checkLandingSpace(checkX:Number,checkY:Number):void
{
if (checkX = (your point) || (another point) //first check all x points...continue like this for all points where a ladder is...the || means or
{
if (checkY = (your points....etc)
{
mc_avatarLadder.play(); //choose your instance name to be mc_avatar, then play the tween
}
}
}
Or check this post for equating one variable to any element in an array If [Get Variable] is equal to [Array]
Now write the that for your ladder. Then do the exact same thing with all of your shoots points. Except, make a new animation with instance name mc_avatarShoot then just say mc_avatarShoot.play();
After that, make sure on the last frame for both instances, you just put in gotoAndStop(1); just to make sure it's ready next time you go to play the animation.
Of course, adjust your timer appropriately. This should pretty much do it, there might be a couple things to adjust for your own game, but follow this and understand it and you'll get it.

How to make a movieclip appear and reappear in the same location?

Well, I'm working on a shooting game in which I have to click enemies to make two counters count the number of hits I get. In this case the enemies are ghosts and I tried making some kind of animation when they are shot so the animation replaces them after I click on them.
However I can't seem to make them reappear again in the same location, it's a very simple drawing made with the shape and drawing tools of Flash CS5.
The code I use in this case for the shooting part is this one:
function disparar (event:MouseEvent):void{
contar +=1;
disparos.text = contar.toString();
var destino1:Boolean = this.mira.hitTestObject(this.FRojo)
if (destino1 == true){
cuenta +=1;
aciertos.text = cuenta.toString();
this.FRojo.visible = false;
colorante.color = 0xFF0000;
this.bang.transform.colorTransform = colorante;
this.bang.x = this.FRojo.x;
this.bang.y = this.FRojo.y;
this.bang.scaleX = this.FRojo.scaleX;
this.bang.scaleY = this.FRojo.scaleY;
this.bang.play();
this.FRojo.visible = true;
}
In fact you can see the whole file in here, it's very simple, but I can't seem to make the movieclip either disappear and reappear or make the animation that follows reappear each time I hit one of the ghosts. Could anyone help me with this? I'd really appreciate the help.
You're hiding the movie clip by setting this.FRojo.visible = false; and then setting it back to true pretty much right after so it never hides.
Try replacing this.FRojo.visible = true;
with
setTimeout(function(){FRojo.visible = true;}, 1000);
That will make it reappear after 1 second (1000 milliseconds).
So remove this and make sure each one is FRojo, FAzul and FMorado.
If you use a tweening library like greensock you can do some cool fades rather than it just reappearing.
EDIT
You have another error inside your BANG movie clip. On frame 10 replace
this.bang.x = 50;
this.bang.y = 10;
this.bang.scaleX = 1;
this.bang.scaleY = 1;
with
this.x = 50;
this.y = 10;
this.scaleX = 1;
this.scaleY = 1;

Actionscript 3: Healthbar and Button

So basically I am making a game in which a button is clicked to decrease the amount of health in a healthbar. I have a button on the stage named fortyfivedown_btn, and a healthbar, which is a 101 frame (includes zero) movieclip. The health bar has an instance name of lifebar. On the stage, the button coding is:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
function fortyfivedownClick(event:MouseEvent):void{
lifebar.health = lifebar.health-45;
}
Inside the healthbar movieclip, I have a layer of coding that is:
var health:int = 100;
gotoAndStop(health + 1);
if(health < 0) health = 0;
else if(health > 100) health = 100;
gotoAndStop(health + 1);
So, there is my coding. The thing is, when the button is clicked, the healthbar does not go down. I traced the health variable in the button:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
function fortyfivedownClick(event:MouseEvent):void{
lifebar.health = lifebar.health-45;
}
{
trace(lifebar.health);
}
I saw that the output is 0. For some reason the button believes the health is 0, when I declared it was 100 inside the healthbar movieclip? Any help is appreciated.
(Edit)
Alright, in answer to the trace question, if I don't do it like that, there is no output. I should say I'm a beginner at this all, and am learning as I go, so please bear with me. Here is my fla file:
https://skydrive.live.com/embed?cid=9AB08B59DCCDF9C6&resid=9AB08B59DCCDF9C6%21107&authkey=AGqFHhlHnvOXvuc
Okay so take the code that was in your lifebar movie clip out entirely, you can just delete that layer for now, then replace the code you have in the main scene with this and you should get the result you want:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
var health:int = 100;
lifebar.gotoAndStop(101);
function fortyfivedownClick(event:MouseEvent):void{
health -= 45;
if(health < 0) health = 0;
else if(health > 100) health = 100;
lifebar.gotoAndStop(health + 1);
//Can write this also: lifebar.health += 45;
trace(health);
}
there's other ways to go about doing this but given your current setup this is going to be the least modification to get what you want. Another option is setting the width on a sprite.
I uploaded a modified fla and companion as file for how I would personally go about doing this instead of including things on the timeline so much:
http://www.mediafire.com/?d38hbm32p71x1n8

Actionscript-3: Making Variables stay the same

I am trying to make a game in which a button is clicked, and 45 health points from a healthbar goes down. I have all my coding, and it works well, but I want to make the button so that if health is less than 45, nothing is taken from the health bar. I tried using:
if(health < 45) health = health;
But it did not work out. I have a feeling the solution to this problem is easy, but I just can't figure it out. Obviously, I am very new at this all, and still find it hard to wrap my head around some concepts. This is my coding:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
var health:int = 100;
lifebar.gotoAndStop(101);
function fortyfivedownClick(event:MouseEvent):void{
health -= 45;
if(health < 0) health = 0;
else if(health > 100) health = 100;
lifebar.gotoAndStop(health + 1);
}
Is there anything wrong with simply doing nothing at all if the health is less than or equal to 45? For example, something like this:
function fortyfivedownClick(event:MouseEvent):void {
if (health <= 45) {
return;
}
// Perform action
}
This will cause the function to exit early if the player doesn't have enough health.
If I understand the question:
if(health>=45) // just add this
lifebar.gotoAndStop(health + 1);
Its pretty simple actually, your event tells your health to go down by 45 and THEN checks if health is below 0, you just need to check how much health you have at the very beginning of the method and jump out of the method if it is 45 or below.
No idea if "break" works in flash, but that would be the easiest solution
eg:
function fortyfivedownClick(event:MouseEvent):void{
if (health <= 45) {
break;
}
health -= 45;
if(health < 0) health = 0;
else if(health > 100) health = 100;
lifebar.gotoAndStop(health + 1);
}
Use Math.max method. IT's very handy places like value normalization.
function fortyfivedownClick(event:MouseEvent):void{
health = Math.max( 45, health -= 45 );
}

How to make smooth moving using as3?

I have loaded some images through XML and attached into dynamically created MovieClips named mc0,mc1,mc2...etc.
_loader.removeEventListener(ProgressEvent.PROGRESS, onLoadingAction);
count++;
var img:Bitmap = Bitmap(e.target.content);
img.cacheAsBitmap = true;
img.smoothing = true;
img.alpha = 0;
TweenLite.to(MovieClip(my_mc.getChildByName("mc"+count)).addChild(img),1, {alpha:1,ease:Quint.easeIn});
and within ENTER_FRAME handler
for (i=0; i < mc.numChildren; i++)
{
my_mc.getChildAt(i).x -= Math.round((mouseX-stage.stageWidth/2)*.006);
}
Everthing works fine. But it is shaking so that it was not looking good.
How do I achieve smooth movement?
One solution I've used is to round the (x,y) position to the closest integer. No matter that you've added smoothing to your bitmap and cached it, rounding could make it feel less choppy and way smoother.
Another thing you need to be careful is the dimensions of the images. Images that have an odd dimension won't be smoothed the same way as images with even dimensions. Check how to workaround this in my blog post Flash Smoothing Issue.
Since Flash has a variable frame rate (in the sense that it will drop frames), one shouldn't depend on the entering of a frame as a unit of action. Rather, it would be wiser to calculate the elapsed time explicitly.
For instance, in the enter frame handler:
var currentTime:Number = (new Date()).time;
for (i=0; i < mc.numChildren; i++)
{
my_mc.getChildAt(i).x -= speed * (currentTime - lastTime); // speed is in px/ms
}
lastTime = currentTime;
where you have the variable lastTime declared somewhere in a persistent scope:
var lastTime:Number = (new Date()).time;
I don't know if this addresses what you are calling "shaking", but it's at least something to consider.