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! :)
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! :)