PDA

View Full Version : Yep, more hitTest/shapeFlag problems.



NuclearDuckie
October 11th, 2008, 07:59 PM
Hi.
I know there are already a lot of these kinds of threads, but I did conduct a search before writing this, though I probably missed something. I'm posting this here anyway, so that I can have this problem in particular addressed, so I hope you don't mind.

So...

I'm trying to create a Flash game engine with a wall that the character bounces off, that has so far come out like this. (http://www.zshare.net/flash/2035290230851eae/)

Or, in image form, this:

http://i43.photobucket.com/albums/e379/carnivorousduckie/Flash%20Hit%20Test/first.jpg

The script on the ball is:


onClipEvent(load){
speed = 1.75
xspeed = 0
yspeed = 0
slow = .875
}
onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
xspeed -= speed
}
if(Key.isDown(Key.RIGHT)){
xspeed += speed
}
if(Key.isDown(Key.UP)){
yspeed -= speed
}
if(Key.isDown(Key.DOWN)){
yspeed += speed
}
if(_root.sidewalls.hitTest(_x, _y, true)){
xspeed *= -1
}
if(_root.updownwalls.hitTest(_x, _y, true)){
yspeed *= -1
}
xspeed *= slow
yspeed *= slow
_x += xspeed
_y += yspeed
}And the wall instances were these:

http://i43.photobucket.com/albums/e379/carnivorousduckie/Flash%20Hit%20Test/second.jpg
http://i43.photobucket.com/albums/e379/carnivorousduckie/Flash%20Hit%20Test/third.jpg

First of all, because I'm using a shapeFlag function instead of a hitTest, because I intend to create games with sloping landscapes, the ball sinks halfway through the wall before hitting it, and I don't know how to create more collision points in the wall.

My main problem however, is that, as you may have noticed, you can sometimes sink further than halfway through the wall, and eventually get onto the other side of it. If I could get it to keep testing the collision wherever the ball was touching the wall, I could make the wall into a kind of one-way thing, where it forces you back into the square, except I don't know how to do that, either.

So could you help me? Or if you think that this question has already been asked, could you post a link to that thread?

Thanks. :}

SparK_BR
October 11th, 2008, 09:55 PM
well...

i will make changes in the code and comment them
ok?
ActionScript Code:

onClipEvent(load){
speed = 1.75
xspeed = 0
yspeed = 0
slow = .875
}
onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
xspeed -= speed
}
if(Key.isDown(Key.RIGHT)){
xspeed += speed
}
if(Key.isDown(Key.UP)){
yspeed -= speed
}
if(Key.isDown(Key.DOWN)){
yspeed += speed
}
if(_root.sidewalls.hitTest(_x -(this._width/2), _y, true)
or _root.sidewalls.hitTest(_x +(this._width/2), _y, true)){

xspeed *= -1
}
if(_root.updownwalls.hitTest(_x , _y -(this._height/2), true)
or _root.updownwalls.hitTest(_x , _y +(this._height/2), true)){

yspeed *= -1
}
xspeed *= slow
yspeed *= slow
_x += xspeed
_y += yspeed
}




now, your main problem is that to make a nice collision you should put the collision code into the walls
and have 4 walls not 2 mcs with 2 walls in each

then you could use the _x and _y of the wall to add to the ball position, keeping it off the wall
(even hittest would be no longer required)

sorry for making poop in your code

NuclearDuckie
October 14th, 2008, 05:44 AM
then you could use the _x and _y of the wall to add to the ball position, keeping it off the wall
(even hittest would be no longer required)

That sounds a lot more reliable, actually, though I don't know how I would write that.
Also would that be able to support the bounce? I'm sure there's a more complicated way to make that bounce work on a wall of any angle, where something calculates the angle of the wall and the angle you hit it from, but that's all a bit beyond me.

SparK_BR
October 15th, 2008, 08:12 PM
your walls are straing the angle is always the same as inverting the axis!

about the hitTest

this is my wall:
ActionScript Code:



onClipEvent(enterFrame){
if(this._x < _root.ball._x - _root.ball._width/2 < this._x + this._width/2){
_root.ball.xspeed = Math.sqrt(_root.ball.xspeed * _root.ball.xspeed);
_root.ball._x -= (this._x + this._width/2) - _root.ball._x - _root.ball._width/2;
}
if(this._x -this._width/2 < _root.ball._x + _root.ball._width/2 < this._x){
_root.ball.xspeed = -Math.sqrt(_root.ball.xspeed * _root.ball.xspeed);
_root.ball._x -= (this._x - this._width/2) - _root.ball._x + _root.ball._width/2;
}

if(this._y < _root.ball._y - _root.ball._height < this._y + this._height/2){
_root.ball.yspeed = Math.sqrt(_root.ball.yspeed * _root.ball.yspeed);
_root.ball._y += (this._y + this._height/2) - _root.ball._y;
}
if(this._y -this._height/2 < _root.ball._y + _root.ball._height < this._y){
_root.ball.yspeed = -Math.sqrt(_root.ball.yspeed * _root.ball.yspeed);
_root.ball._y -= (this._y - this._height/2) - _root.ball._y;
}

}




the ball is pretty much the same as yours