PDA

View Full Version : Another hitTest thread...


ChaosResolution
08-04-2009, 09:43 PM
Hi all! I'm having a bit of difficulty with a small game I'm making in Flash CS4. I had a look at some other users who had similar problems, but I couldn't quite translate it to my program. Basically, the user can move a little golf cart around while several golf balls randomly appear with increasing amounts about the place. The idea is to pick up the balls by moving the cart over them, by means of a hitTest. The problem is I'm just not sure exactly how to go about it - I gave it a go and got errors :rolleyes: So, I've got 3 .as classes. DrivingRange, GolfCart and GolfBall. I think DrivingRange is what you'd call the document class... I think. Here it is, with my failed attempt at the hitTest commented out.

package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
import flash.utils.Timer;
import flash.text.*;

public class DrivingRange extends MovieClip
{
private var count:uint;
private var prevCount:uint;
private var timer:Timer;
private var scoreText:TextField;
private var score:uint;

public function DrivingRange()
{
var myGolfCart:GolfCart = new GolfCart(stage);

stage.addChild(myGolfCart);

myGolfCart.x = stage.stageWidth / 2;
myGolfCart.y = stage.stageHeight / 2;
//addEventListener(Event.ENTER_FRAME, pickUp);

init();
}

private function init():void
{
var myGolfBall:GolfBall = new GolfBall();

myGolfBall.x = Math.random() * stage.stageWidth;
myGolfBall.y = Math.random() * stage.stageHeight;

addChild(myGolfBall);
count ++;
timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, addGolfBalls);
timer.start();

scoreText = new TextField();
scoreText.x = 500;
scoreText.y = 10;
scoreText.width = 200;
addChild(scoreText);
}

/*private function pickUp(event:Event):void
{
var myGolfBall:GolfBall = new GolfBall();
var myGolfCart:GolfCart = new GolfCart(stage);

if(myGolfCart.hitTestObject(myGolfBall))
{
myGolfBall.collect();

score += 10;
scoreText.text = score.toString();
}
}*/

private function addGolfBalls(e:TimerEvent)
{
prevCount = count;
count += count;

for (var i:int = prevCount; i < count; i++)
{
var myGolfBall:GolfBall = new GolfBall();

myGolfBall.name = "golfball" + i;
myGolfBall.x = Math.random() * stage.width;
myGolfBall.y = Math.random() * stage.height;
addChild(myGolfBall);
}

overrun();
}

private function overrun()
{
if (numChildren > 250)
{
timer.stop();
var i:int = this.numChildren;

while(i--)
{
removeChildAt(i);
}

addChild(scoreText);

}
}
}
}

And here's my GolfBall class, with more failed attempt work commented out:

package
{
import flash.display.MovieClip;
import flash.events.*;

public dynamic class GolfBall extends MovieClip
{

public function GolfBall()
{

}

public function collect()
{
//this.removeEventListener(Event.ENTER_FRAME, pickUp);
parent.removeChild(this);
}
}
}

So if anyone would like to go ahead and tell me where I'm coding gibberish and what I should be doing instead, I'd really really appreciate it! :)

backroomwebdesign.com
08-04-2009, 09:46 PM
What r ur errors?

ChaosResolution
08-04-2009, 10:03 PM
With just the pickUp function uncommented - absolutely nothing. The golfCart MC just goes over the balls. With the addEventListener also uncommented, I get
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at GolfBall/collect()
at DrivingRange/pickUp()
thrown up repeatedly, while the framerate of the game drops drastically.

backroomwebdesign.com
08-04-2009, 10:06 PM
I think it thinks the stage does not exist. Try trace(stage); in various parts see if it exists (not null) where you think it shouldnt be.

ChaosResolution
08-04-2009, 10:18 PM
I think it thinks the stage does not exist. Try trace(stage); in various parts see if it exists (not null) where you think it shouldnt be.

I'm afraid I don't quite understand what that's supposed to do or what I'm looking for :confused:

backroomwebdesign.com
08-05-2009, 02:09 AM
You show 2 classes, 1 of which uses the stage. How is that class supposed to know what the stage is? If you reference something that dosent exist, you get this error.

The thing to do is pass the stage in the constructor, then in the constructor assign it to a variable of the class. Then you can reference the stage.

UncleNinja
08-05-2009, 10:58 AM
Where's your GolfCart class?
I'm trying to figure out how your code works, but I can't fully understand it without that class. :)

ChaosResolution
08-05-2009, 11:18 AM
Where's your GolfCart class?
I'm trying to figure out how your code works, but I can't fully understand it without that class. :)

The code in here deals with the MC's movement:
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.*;
import flash.ui.Keyboard;
import com.senocular.utils.KeyObject;

public dynamic class GolfCart extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;

public function GolfCart(stageRef:Stage)
{
this.stageRef = stageRef;
key = new KeyObject(stageRef);

addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}

public function loop(e:Event) : void
{
if (key.isDown(Keyboard.LEFT))
{
y -= 3;
x -= 6;
gotoAndStop("left");
}

else if (key.isDown(Keyboard.RIGHT))
{
y += 3;
x += 6;
gotoAndStop("right");
}

if (key.isDown(Keyboard.UP))
{
y -= 3;
x += 6;
gotoAndStop("up");
}

else if (key.isDown(Keyboard.DOWN))
{
y += 3;
x -= 6;
gotoAndStop("down");
}
else if (!key.isDown(Keyboard.DOWN) && !key.isDown(Keyboard.UP) && !key.isDown(Keyboard.RIGHT) && !key.isDown(Keyboard.LEFT))
{
gotoAndPlay("down");
}
}
}
}

ChaosResolution
08-05-2009, 04:52 PM
So can anybody figure out how to do what I want to do? :p

UncleNinja
08-05-2009, 09:21 PM
I'm playing with your code, but I have a question; what is KeyObject? Can I see the code for that, too? :)
*laughs evilly; next he will be wanting the class for ChaosResolution's bellybutton lint*

ChaosResolution
08-06-2009, 09:13 AM
I'm playing with your code, but I have a question; what is KeyObject? Can I see the code for that, too? :)
*laughs evilly; next he will be wanting the class for ChaosResolution's bellybutton lint*

Voila (http://www.senocular.com/flash/actionscript.php?file=ActionScript_3.0/com/senocular/utils/KeyObject.as)