views:

299

answers:

2

For starters: I'm working with Flash CS3 and Actionscript 2.0

I'm trying to remake the frogger game, and I'm kinda stuck with putting the cars on the screen.

For those of you who don't know frogger: http://www.actionscript.org/showMovie.php?id=1157, but I'm not implementing the logs.

The big problem is that I have 3 cars, all of which are movieclips in the library, I won't place any of them on the stage. Multiple instances must appear at the same time. These cars need to drive in both directions and all 3 cars must appear in all lanes(I have 4 lanes) Offcourse, 1 lane only has 1 direction.

I can use hitTest() to see if my frog has hit one of the cars, but I need to make it realistic, meaning I can't hard-code the amount of cars on each lane.

I also can't seem to find how to rotate a movieclip around its center...

+2  A: 

Well, there are a couple things I can suggest.

For the cars, create each car as a separate object in the Library (don't put them on the stage). When you need to have one appear on the screen use attachMovie() to create an instance of the car you want.

For making them face different directions (I believe you just need left and right), you could either create 2 copies of each car in the Library, one facing each direction. Or, I think if you set xScale value to -100% I think that will flip it horizontally, so you could use the same Library instance for both. However, it would probably be easier to deal with if you had separate Library instances for each direction, instead of doing rotations on one.

Herms
Thanks. The asignment allready said that only the background could be on the stage, meaning all cars are in the library.
WebDevHobo
+1  A: 

to answer your question about rotation, movie clips rotate around their registration point, not around their visual center. So when you create your clips, make sure that the crosshairs on the symbol-editing screen appear in the center. The crosshairs is the registration point, which basically defines where x:0,y:0 is on the clip.

It sounds like your question is really about how to use hitTest to see if the frog has hit any of the cars, regardless of which one, how many are on stage, etc. So what I would do is create a class for the car with a static member that can be a pointer to the frog, and then have the class check for whether it is hitting the frog.

So to start out:

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 car, you can extend the Car 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 the individual classes, use Linkage on your Library symbols for each car. (rightclick on the symbol, select Linkage, and type your class name in the Class field).

Finally, set the frog member 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 themselves for whether they're hitting the frog.

The other option is to code that checkHit() function on the first frame of each different car movieclip, rather than using 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;
    }
}
nerdabilly