Introducing frogger with flash
For starters: I'm working with Flash CS3 and ActionScript 2.0
I'm trying to remake a frogger game and I'm kind of stuck with putting cars on the screen.
For those of you who don't know frogger: http://www.actionscript.org/showMovie.php?id=1157 but I am not embedding logs.
The big problem is that I have 3 cars, all of which are movie clips in the library, I will not place them on stage. Several copies should appear at the same time. These cars must drive in both directions and all 3 cars must appear on all lanes (I have 4 lanes) Of course, 1 lane only has 1 direction.
I can use hitTest()
to see if my frog hit one of the cars, but I need to make it realistic, meaning I cannot hardcode the number of cars in each lane.
I also can't seem to find a way to rotate the movie clip around its center ...
source to share
Well, there are a few things I can suggest.
For cars, create each car as a separate object in the Library (don't put them in the scene). When you need one of them to appear on the screen, use attachMovie () to instantiate the car you're interested in.
In order to make them face different directions (I believe you just need left and right), you can either create 2 copies of each car in the Library, one in each direction. Or, I think if you set the xScale value to -100%, I think it will flip it horizontally, so you can use the same library instance for both. However, this would be easier to deal with if you had separate instances of the library for each direction, instead of doing rotations on one.
source to share
to answer your question about rotation, movie clips rotate around their registration point, not around their visual center. Therefore, when you create clips, make sure the crosshairs appear in the center in the symbol editing screen. The crosshair is the registration point that basically defines where x: 0, y: 0 is in the clip.
It looks like your question is really about how to use hitTest to see if a frog has hit any of the cars, no matter which one, how many in the scene, etc. So what I would do is create a class for a car with a static member that can be a pointer to a frog, and then check the class if it hits the frog.
So to get started:
public class Car extends MovieClip{
public static var frog:MovieClip;
private var interval;
public function Car(){
super();
interval = setInterval(checkHit,500);
}
private function checkHit(){
if(this.hitTest(frog)){
trace("the frog hit the car");
clearInterval(interval);
//do what you need to do when the frog gets hit
}
}
}
For each individual vehicle, you can expand the vehicle class:
class Truck extends Car{
public function Truck(){
super();
}
}
class Volkswagen extends Car{
public function Volkswagen(){
super();
}
}
class Bus extends Car{
public function Bus(){
super();
}
}
After creating individual classes, use Linkage on your library symbols for each vehicle. (right click on the symbol, select Link and enter the class name in the Class field).
Finally, set the frog to your frog on stage
var frog:MovieClip = attachMovie("frog_mc", frogMC, _root.getNextHighestDepth())
Car.frog = frog; //set the static var "frog" to your frog instance
And now your cars should all check to see if they hit the frog.
Another option is to program the checkHit () function on the first frame of each different car, rather than classes for each:
this.onEnterFrame = function(){
if(this.hitTest(_root.frog)){
trace("the frog hit the car");
//do what you need to do when the frog gets hit
delete this.onEnterFrame;
}
}
source to share