PDA

View Full Version : [AS3] Lives error


ElGamble
01-23-2012, 12:35 AM
Hi guys, I've used this code to show the lives, and how they go down when a collision occurs:

var lives:Number = 3;
Lives.text=String(lives);
trace (lives);
if (lives < 0)
{gotoAndPlay("gameover");}
if (dragon_mc.hitTestObject(dwarf_mc)){
lives= lives -1;
Lives.text=String(lives);
trace (lives);
if (lives < 0)
{gotoAndPlay("gameover");}

When I play it, there are no errors, but the lives do not get deducted when a collision occurs.
I've tried placing it within this code here:

function checkCollision(evt){
if (dragon_mc.hitTestObject(dwarf_mc)){
stage.removeEventListener(Event.ENTER_FRAME, checkCollision)
up = down = false;
dragon_mc.visible = false;
dwarf_mc.visible = false;
dragonTimer.start();
death_mc.x = dragon_mc.x
death_mc.y = dragon_mc.y
death_mc.visible = true;
death_mc.gotoAndPlay(1);
This seems to work, but when the character gets hit at times, the lives go down by 2, rather than one.

What can I do? :)

iamgotenks
01-23-2012, 01:53 AM
Well, I'm assuming your code is inside an enterframe event loop, so in that case, you need to move your var lives:int = 3; outside your event function, basically you're setting the lives = 3 every single frame before you do a hit test and decrease the lives. Just move your variable declaration to the start of your code (outside the enterframe).

ElGamble
01-23-2012, 02:08 AM
Sorry I'm quite new to Actionscript, that didn't work for me, I don't think it's in an enter loop, could you give me an example of a functioning code that exists in an event loop please? I'm still not completely familiar with terms etc.
Thank you :)

Mazoonist
01-23-2012, 02:41 AM
Code that is not in your enter frame handler will only execute once, up front. Therefore, in your first example, all that code you wrote only executes once, and all of the if conditions that you wrote there will all evaluate to false, they don't have a hope of evaluating to true because they only execute once, and blazingly fast, too, so there is just simply not enough time. When your program starts, the objects in question are not hitting, and all checking ends there.

What you need to do is write an enter frame function, which gets called repeatedly, and do your hit testing in there. You also need to use the enter frame function to make your objects move. Anyway, I have written a whole tutorial on this, which you can find here: http://theflashconnection.com/content/flash-as3-left-right-shooter-game

If it's not the kind of game you were wanting to make, that's not the point. At least it gives you the basics of making a simple game and explains it very thoroughly.

ElGamble
01-23-2012, 02:42 AM
I tried sorting it, it seemed fine for a while then I noticed a lot of the features were acting strangely. This is what I have currently

var lives:int = 20;
var score:int = 0;
score_txt.text = score.toString();
Lives.text = score.toString();


addEventListener(Event.ENTER_FRAME,onLoop);

var timer:Timer = new Timer(1000, 50);
timer.addEventListener(TimerEvent.TIMER, tallyScore);
timer.start();


function tallyScore(e:TimerEvent):void
{
score++;
score_txt.text = score.toString();
}

function onLoop(e:Event):void
{
if (dragon_mc.hitTestObject(dwarf_mc))
{


timer.stop();

timer.reset();
score = 0;
score_txt.text = score.toString();

timer.start();
}
if (score > 15) {

gotoAndPlay("gameover");}

else if(dragon_mc.hitTestObject (dwarf_mc))

lives= lives -1;
Lives.text=String(lives);

if (lives < 0)
{gotoAndPlay("gameover");}
}

ElGamble
01-23-2012, 02:49 AM
Ah thank you, I looked at you're tutorial, and was wondering if there is anyway of doing it without array?
Thank you so much for your help!

Mazoonist
01-23-2012, 02:55 AM
I have always done my best to explain arrays and make them easier to understand. All it really is, is just a series of indexed names, or references. Just like instance names.

I have also created an interactive demo of how for loops work (it's in that tutorial).

I don't know what else I could do to make it easier to understand, but if I could I would. The whole question is too complex to answer in one little forum post, so that's why I write tutorials. I don't know everything, and I don't specialize in games, but I try to share back what I have figured out.

You could make a game without arrays and loops if you are happy with one hero, one enemy, and one bullet, but chances are you won't be. :rolleyes: