17 Replies Latest reply on May 8, 2006 5:59 AM by blemmo

# Need help

Hi everyone. I have just started learning flash, and with the help of my teacher and some internet tutorials, I have started creating a cargame that is seen from above.

I want my car to bounce off a wall (vagg), and go slower on grass (gras).
I have succeeded in this, but as soon as I do two "gras" or "vagg", one of them stop working. Why?

Any help is appreciated...

• ###### 1. Re: Need help
Hittests are done with a specific MC instance, so if you want to test more MCs, you have to add a hittest for each:
if(this.hitTest(_root.vagg1) || this.hitTest(_root.vagg2)) {
...
}
The '||' stands for logical or (I think you can also write 'or' instead), so it will execute the code inside if one of the walls was hit (wall 1 or wall 2).

hth,
blemmo
• ###### 2. Re: Need help
So, it has to be a unice MC? It has to be possible to make several ones have the same effect... Or am I wrong? I don't want to write 100 or's just to be able to make a circuit for the car...
• ###### 3. Re: Need help
Well, you could try to use the 2nd hitTest function and place all walls in a big MC (if that's possible in your movie). Then you would have to check the 4 corner coordinates of the car in the hitTest and set the shapeFlag parameter to true, so it doesn't check for the wall-MC's bounding box, but only for the parts that contain something, leaving out the free space. Should look like this:
walls_mc.hitTest(car._x, car._y, true)
walls_mc.hitTest(car._x+car._width, car._y, true)
walls_mc.hitTest(car._x, car._y+car._height, true)
walls_mc.hitTest(car._x+car._width, car._y+car._height, true)

Another possibility is to have all wall MCs in an array and loop through this, doing a hitTest with each entry, but I guess this may effect performance if there are a lot of single MCs.

greets,
blemmo
• ###### 4. Need help
What do I apply the text to? the car?

The other code ( I posted at the first thread) was in the car... But making the mc shape sounds nice...
• ###### 5. Re: Need help
It doesn't really matter where the code is placed, as long as the paths to the MCs are ok and it gets called repeatedly. I like to have most code in the timeline, so I'd place it in a onEnterFrame or setInterval in the actions of a frame in the timeline that holds walls_mc and car and attach it to the car mc's enterFrame event:

car.onEnterFrame = wallHitTest;

function wallHitTest(){
if ( walls_mc.hitTest(car._x, car._y, true) ) { ... } // regpoint hit wall
if ( walls_mc.hitTest(car._x+car._width, car._y, true) ) { ... } // topright
if ( walls_mc.hitTest(car._x, car._y+car._height, true) ) { ... } // bottomleft
if ( walls_mc.hitTest(car._x+car._width, car._y+car._height, true) ) { ... } // bottomright
}

greets,
blemmo
• ###### 6. Need help
Talk to me as if I am 5 years old...

No but seriously... I'm having a hard time understanding.
Where do I place the code and how, and what do i change to make it work?

Ok, I managed to find a script where when the car touches the true form of the mc, it says Hit and otherwise No hit... But the car still slows down over all of it... If you understand what I mean...
• ###### 7. Re: Need help
Well, as I wrote: in a frame of the timeline containing the wall and car mcs, or if you want to stick with onClipEvent(enterFrame) of the car mc, place it there:

onClipEvent(enterFrame) {
if ( this._parent.walls_mc.hitTest(this._x, this._y, true) ) { ... } // regpoint hit wall
if ( this._parent.walls_mc.hitTest(this._x+car._width, this._y, true) ) { ... } // topright
if ( this._parent.walls_mc.hitTest(this._x, this._y+this._height, true) ) { ... } // bottomleft
if ( this._parent.walls_mc.hitTest(this._x+this._width, this._y+this._height, true) ) { ... } // bottomright
if (d) {
if (acc = 1,5) {
acc--;
}
}
}

walls_mc and car are in the same parent MC in this code. Also note that the regpoint of MC 'car' should be the top left corner for this hittests.
• ###### 8. Re: Need help
I get this error:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Clip events are permitted only for movie clip instances
onClipEvent(enterFrame) {

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Unexpected '.' encountered
if ( this._parent.walls_mc.hitTest(this._x, this._y, true) ) { ... } // regpoint hit wall

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Unexpected '.' encountered
if ( this._parent.walls_mc.hitTest(this._x+car._width, this._y, true) ) { ... } // topright

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Unexpected '.' encountered
if ( this._parent.walls_mc.hitTest(this._x, this._y+this._height, true) ) { ... } // bottomleft

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Unexpected '.' encountered
if ( this._parent.walls_mc.hitTest(this._x+this._width, this._y+this._height, true) ) { ... } // bottomright

Total ActionScript Errors: 5 Reported Errors: 5

• ###### 9. Re: Need help
onClipEvent has to be attached to a MovieClip instance, not to a timeline frame. If you want to attach the code to a frame, use
mcinstance.onEnterFrame = function() {
...
}
or the code from the 2nd post before this one.

I don't know if you can have xxx.onEnterFrame = ... and onClipEvent(enterFrame) on that mc simultaneously, I guess you have to decide which one you want to use. So all code running in the onEnterFrame event of the car mc should be either in onEnterFrame on the timeline or in onClipEvent(enterFrame) on the mc. These are 2 different ways, so better don't mix it.
• ###### 10. Re: Need help
But if I place the code on the wall? Would that work? I'm really confused...
• ###### 11. Re: Need help
Help?
• ###### 12. Re: Need help
You could attach the script to the wall also, sure. Should look like this then:
if ( this.hitTest(this._parent.car._x, this._parent.car._y, true) )
and similar for the other 3 points of the car mc's bounding box.
• ###### 13. Re: Need help
What do I have to replace/change in the script?
• ###### 14. Re: Need help
What do I have to replace/change in the script?
• ###### 15. Re: Need help
What do I have to replace/change in the script?

I know I'm being a pain, and I am extremly sorry, but I have no experience in this program at all, and I really need it to work...

Thanks for understanding.
• ###### 16. Re: Need help
Pleaaaaaaase help me... I'm terribly sorry about all this...
• ###### 17. Re: Need help
Please specify what's the problem again, and post some of the code you use now.