PDA

View Full Version : [AS3] Using Score In Game


glovaxlr
07-08-2009, 11:35 PM
Hey, new to the boards here. I am currently in a flash gaming class, and I am making a game, obviously. Anyways what I'm trying to accomplish is when the score goes to zero I want the game to end and go to the gameover screen.

this is the specific section of code that I have been experimenting with.

if (ScoreField.text == "Life: 0")
{
gotoAndPlay("fail");
}


This code has currently done nothing. And any variation I've created has only created errors. Any suggestions on how to do this correctly will be greatly appreciated. Thanks!

jakesee
07-08-2009, 11:42 PM
Erm... so what is your question?

finaiized
07-09-2009, 12:12 AM
It's hard to find the problem without actually knowing what is what. For all we know, scorefield has nothing to do with anything. You could have been using the wrong textfield, etc. It'd be better to upload the .fla or at least give us way more detail.

glovaxlr
07-09-2009, 12:15 AM
this is all the actionscript of the file
textfield is the name of a dynamic text box that the score is shown in. it begins at 100, when it gets to 0, i want the game to be over




var score = 100;

import flash.ui.Mouse;
Mouse.hide();
function init()
{
Mouse.hide();

//respond to mouse move events
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}

function mouseMoveHandler(evt:MouseEvent):void
{
//whenever the mouse moves, place the cursor in the same spot
circle.x = evt.stageX;
circle.y = evt.stageY;
}



var myText:TextField = new TextField();
myText.text = "You got this, c'mon let's keep it up!";


addEventListener(Event.ENTER_FRAME, checkCollision);

function checkCollision(event:Event){

//check the cursor location against the rectangle
if(rectangle.hitTestPoint(mouseX, mouseY, true)){
messageText1.text ="";
{
ScoreField.text = "Score: "+(score=50);

}

}else{
messageText1.text = "";
ScoreField.text = "Life: "+(score-=1);
}

//move circle with mouse
circle.x = mouseX;
circle.y = mouseY;

//test circle versus rectangle
/*if(circle.hitTestObject(rectangle)){
messageText2.text = "hitTestObject: YES";

addChild(myText);



}else{

addChild(myText);

removeChild(myText);
messageText2.text = "hitTestObject: NO";


}*/
}

//fail game, score goes negative and sent to fail screen

if (ScoreField.text == "Life: 0")
{
gotoAndPlay("fail");
}

finaiized
07-09-2009, 12:16 AM
Since the variable score holds the score too, use that.
if (score==0) {
gotoAndPlay("fail");
}

You can use [.as] yourcodehere[/.as] to make your code highlighted. Remove the dots.

glovaxlr
07-09-2009, 12:25 AM
I replaced


if (ScoreField.text == "Life: 0")
{
gotoAndPlay("fail");
}

with


if (score==0)
{
gotoAndPlay("fail");
}

but still, nothing has changed. When the score hits zero it just keeps going down into negative numbers, rather than playing the frame under label "fail" which is the game over screen.

(thanks for the wrap tip, the as looks better like that :)

finaiized
07-09-2009, 12:27 AM
Do you mind uploading your .fla so I can have a look at it? If you are looking for a place to host it, http://www.willhostforfood.com can host it for free.

glovaxlr
07-09-2009, 12:36 AM
http://willhostforfood.com/?Action=download&fileid=73791

the fla

and

http://glovaxlr.com/spotlight.html

the swf

finaiized
07-09-2009, 12:43 AM
Fixed it for you. It's because your code wasn't in an enter_frame event listener. The code only ran once when it started- and it definitely wasn't 0 then. The enter_frame runs every frame, so it always checks to make sure it's 0. When you complete the game, you'll get output errors since the function checkCollision() hasn't been removed. Use removeEventListener to do that (check my example, it's on the top).

http://willhostforfood.com/access.php?fileid=73792

EDIT: Remember to remove your current if statement for checking 0 at the end of the code.

glovaxlr
07-09-2009, 12:46 AM
oh wow, awesome, you rock! thank you I appreciate it, for fixing, and for the quick lesson

finaiized
07-09-2009, 12:47 AM
No problem. PM me if you need further help on the checkCollision error.

jakesee
07-09-2009, 03:18 AM
Wow. that game is difficult.

finaiized
07-09-2009, 03:28 AM
Wow. that game is difficult.

Hint: Open the .fla and go to the enemy's timeline. It's a motion path- he takes the same path every time! xD