Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-04-2010, 10:49 AM   #1
Ben91
Registered User
 
Join Date: Mar 2010
Posts: 5
Default [AS3] hitTestObject

Hi i've been making skeetshoot game as part of an assignment.

However the hitTestObject part is not working exactly how i want it.
When a bullet hits a gameskeet or gameskeet2 i want the score to increase by 10 and it to be displayed in the dynamic text. It does this but then goes back to 0.

I'm not sure if it's because i've put some if statements in to get past the problem with some objects being null as at times they aren't on the stage.

I'd really appreciate some help with this as i'm stuck at what to do.

I've put the section of code below. I apologise if it isn't concise- I'm a flash noob xD.

Thanks, Ben.

Code:
function updateGameScore():void {
        score+=10;
        gameScore.text = String(score);
}

stage.addEventListener(Event.ENTER_FRAME, collisionDetector);

function collisionDetector (event:Event):void {
        if (bullet !=  null) 
        {
                if (gameskeet != null)
                {
                        if (gameskeet.hitTestObject(bullet))
                        {
                                if (!collisionOccurred)
                                {
                                        updateGameScore();
                                        collisionOccurred = true;
                                        removeChild(gameskeet);
                                }
                        }
                        else
                        {
                                collisionOccurred = false;
                        }
                }
                else if (gameskeet2 != null)
                {
                        if (gameskeet2.hitTestObject(bullet))
                        {
                                if (!collisionOccurred)
                                {
                                        updateGameScore();
                                        collisionOccurred = true;
                                        removeChild(gameskeet2);
                                }
                        }
                        else
                        {
                                collisionOccurred = false;
                        }
                }
        }
}
Ben91 is offline   Reply With Quote
Old 04-04-2010, 09:51 PM   #2
Impacatus
Senior Member
 
Join Date: Jan 2009
Posts: 163
Default

Doesnt look like anything in that code should be setting it to zero.

Is it the "score" variable getting reset, or just the gameScore textbox?

Try doing a search (ctrl+f) for every time the variable is mentioned, and see if you can figure out where it's getting reset. If that doesn't work, try commenting out each reference one by one until you find the one that stops this from happening.
Impacatus is offline   Reply With Quote
Old 04-04-2010, 11:41 PM   #3
Ben91
Registered User
 
Join Date: Mar 2010
Posts: 5
Default

Just realised i'd missed off the start of that piece of code. Does the first bit of this make any difference?

Code:
var score:int;
var collisionOccurred:Boolean;
gameScore.text = "0";

score = 0;
collisionOccurred = false;

function updateGameScore():void {
	score+=10;
	gameScore.text = String(score);
}

stage.addEventListener(Event.ENTER_FRAME, collisionDetector);

function collisionDetector (event:Event):void {
	if (bullet !=  null) 
	{
		if (gameskeet != null)
		{
			if (gameskeet.hitTestObject(bullet))
			{
				if (!collisionOccurred)
				{
					updateGameScore();
					collisionOccurred = true;
					removeChild(gameskeet);
				}
			}
			else
			{
				collisionOccurred = false;
			}
		}
		else if (gameskeet2 != null)
		{
			if (gameskeet2.hitTestObject(bullet))
			{
				if (!collisionOccurred)
				{
					updateGameScore();
					collisionOccurred = true;
					removeChild(gameskeet2);
				}
			}
			else
			{
				collisionOccurred = false;
			}
		}
	}
}
Ben91 is offline   Reply With Quote
Old 04-05-2010, 05:46 PM   #4
Impacatus
Senior Member
 
Join Date: Jan 2009
Posts: 163
Default

Hmm, not that I can see, unless this section:
ActionScript Code:
score = 0; collisionOccurred = false;
...is being executed more than once for some reason. Maybe you can try putting a trace next to it and see.

When you say it goes back to zero, when do you mean? Unless you mean after you close the swf and restart it, I don't see anything in that code that should cause it.
Impacatus is offline   Reply With Quote
Old 04-05-2010, 09:05 PM   #5
mecopius
Registered User
 
Join Date: Apr 2010
Posts: 5
Default

Where is this code? If this code is on a frame and the frame is being called again, then that could be the reason the score is being set back to 0.
mecopius is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:44 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.