Actionscript-3: Making Variables stay the same - actionscript-3

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 );
}

Related

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

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.

Changing the value of a Number Bug AS3

Hey everyone so I am having some trouble here. Been at it for an hour now and can't find a solution.
So I Have a movie clip named _Bunny added to the stage like so:
_Bunny = new mcBunny;
stage.addChild(_Bunny);
_Bunny.x = (stage.stageWidth / 2) - 225;
_Bunny.y = (stage.stageHeight / 2) - 330;
Now what this _Bunny does is move across the stage horizontally from right to left in a loop which i have set up like so in a Enter_Frame event listener:
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
It's speed is the nBunnySpeed which is equal to 5. Now I have another function that I am trying to change the value of the nBunnySpeed to say 20 whenever the nScore is equal to 1 like so:
private function updateDifficulty():void
{
if (nScore >= 1)
{
//Increase Speed
nBunnySpeed = 20;
}
but the bug that in which is produces is the Bunny shooting off to the right side of the screen which is the "+x" no matter what I do this always happens.
Can anyone see what I might be doing wrong? I don't understand why this is happening. Please help!
You need some kind of a flag that indicates that the difficulty has been already updated. Then, when you call updateDifficulty it first checks if there's a real need to update speed now, if there's none, it just returns. If yes, however, then you update your bunny's speed and set that flag so that the next time the function will not alter the bunny's speed.
var diffUpdated:Boolean=false;
private function updateDifficulty():void
{
if (diffUpdated) return; // here
if (nScore >= 1)
{
//Increase Speed
if (nBunnySpeed<0) nBunnySpeed=-20;
else nBunnySpeed = 20; // retain the direction of bunny's movement
}
diffUpdated=true;
}
Now, whenever you want your difficulty to be updated, you do diffUpdated=false; and voila, bunny's speed will be updated by this. For this, however, you will need more than two levels of speed, maybe one for 10 score, one for 50 and one for say 200.
What you do in this function
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
is check whether the _Bunny is offscreen or not. And if so, the nBunnySpeed will be nBunnySpeed - 1. But since BunnySpeed = 20, it will be 20 + 19 + 18 + 17, still going right. If you'd to turn it to BunnySpeed = -BunnySpeed, it will reverse immediately and go back.

Nape Moving Platform

Okay Im relatively new to nape and Im in the process of making a game, I've made a Body called platform of type KINEMATIC, and I simply want to move it back a forth in a certain range on the stage. Can somebody please see where im going wrong , thanks.
private function enterFrameHandler(ev:Event):void
{
if (movingPlatform.position.x <= 150 )
{
movingPlatform.position.x += 10;
}
if (movingPlatform.position.x >= 260)
{
movingPlatform.velocity.x -= 10;
}
}
First of in one of the if blocks you are incrementing position.x by 10 in the other one you are decrementing velocity.x by 10. I guess you meant position.x in both.
Secondly, imagine movingPlatform.position.x is 150 and your enterFrameHandler runs once. movingPlatform.position.x will become 160 and on the next time enterFrameHandler is called none of the if blocks will execute since 160 is neither less than or equal to 150 or greater than or equal to 260.
You can use the velocity to indicate the side its moving and invert it once you go beyond an edge, something like :
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150 || movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -movingPlatform.velocity.x;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
Obviously this might cause problems if the object is already at let's say x=100, it will just keep inverting it's velocity, so either make sure you place it between 150-260 or add additional checks to prevent it from inverting it's direction more than once.
This might be a better way of doing it :
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150) {
movingPlatform.velocity.x = 1;
} else if (movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -1;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
In general:
Kinematic bodies are supposed to be moved solely with velocity, if you change their position directly then they are not really moving as much as they are 'teleporting' and as far as the physics is concerned their velocity is still exactly 0 so things like collisions and friction will not work as you might expect.
If you want to still work with positions instead of velocities, then there's the method setVelocityFromTarget on the Body class which is designed for kinematics:
body.setVelocityFromTarget(targetPosition, targetRotation, deltaTime);
where deltaTime is the time step you're about to use in the following call to space.step();
All this is really doing is setting an appropriate velocity and angularVel based on the current position/rotation, the target position/rotation and the amount of time it should take to get there.

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

How do I move and scale properly in ActionScript?

function scale(e:Event):void{
plane1_mc.scaleX += .1;
plane1_mc.scaleY += .1;
plane1_mc.x -= 5;
plane1_mc.y -= 3;
}
function prop(evt:Event):void{
plane1_mc.prop_mc.rotation += 100;
}
plane1_mc.prop_mc.addEventListener(Event.ENTER_FRAME, prop);
plane1_mc.addEventListener(Event.ENTER_FRAME, scale);
this is what im using to try to get plane1_mc to scale and move. it was doing both before but now its only doing the scale. Anybody feel free to tell me
Your code is correct. Try increasing the amount you move it and post what happens. Something like this:
plane1_mc.x -= 50;
plane1_mc.y -= 30;
Also make sure you aren't scaling plane1_mc anywhere else. It's possible that another event is being fired instead of this one, so it looks like the scaleX and scaleY values are being modified from this function when they really aren't. I'd suggest putting some trace statements in this function and seeing if they show up.