PDA

View Full Version : Actionscript: HitTest with one symbol



Bonk
04-26-2008, 04:29 AM
I'm making a game blah blah blah, but I have an issue that I hope is simple to solve.

There is the main character, and lining the screen are four graphics of walls within one symbol. The problem is, because it is in one symbol, the whole screen is technically this symbol and the four directional hitTests trigger all the time, so the character can't move.

Is there some way to make hitTest only register for graphical hits, and not empty space created by the symbol's box?

And I want to avoid clunky code which has a separate hitTest for every wall.

Scorpioxxx
04-26-2008, 05:56 AM
the seperate wall parts is the only way I can think of. You should just make them all the same instance name then put in the frame the code for the hero hitting the wall or whatever.

Bonk
04-26-2008, 08:35 AM
You can't give more than one symbol the same instance name...

Steyene
04-26-2008, 09:01 AM
If the frame isn't moving, you can make the character appear on the other side of the frame, by using this code


If(this._x > stagex){
//stagex is a global variable on the frame and is equal to the maximum x value of the frame
this._x = 0;
}
ElseIf(this._x < 0){
this._x = stagex;
};


You could also use AS to draw the boundaries in but that is a lot of work. >_>

Scorpioxxx
04-26-2008, 09:16 AM
You can't give more than one symbol the same instance name...

I can, dunno why you can't.

Bonk
04-26-2008, 06:47 PM
If the frame isn't moving, you can make the character appear on the other side of the frame, by using this code


If(this._x > stagex){
//stagex is a global variable on the frame and is equal to the maximum x value of the frame
this._x = 0;
}
ElseIf(this._x < 0){
this._x = stagex;
};


You could also use AS to draw the boundaries in but that is a lot of work. >_>

Nah, I'm planning a bigger game where there is a scrolling landscape with more walls, so that wouldn't work.

Hmm, maybe I'll try "drawing the boundaries". I'll have to look it up though.


I can, dunno why you can't.

You can give them the same instance name, but only the original registers in code.

Rather Cheesy
04-26-2008, 07:52 PM
The easiest way to to make 4 different walls and put the code on the wall. So if the code on the character is like this

onClipEvent(enterFram){
If(this.hittest(wall1)){
_x = 10
}
}

Change it to smoething like this and put it on the wall

onClipEvent(enterFram){
If(this.hittest(player)){
_x = 10
}
}

I just chose _x = 10 as a random movement code, you can make it whatever you want.

EDIT:

The plus side of putting the hittest on the wall is that you don't have to give instance names to them all, once you make one you can copy and paste the rest. with only having to give one instance name to the player.

Kitsune
04-26-2008, 08:22 PM
Steiner's solution would work even if you had a bigger game. Just set the stagex and shit variables on each frame the boundaries change.

Bonk
04-26-2008, 08:47 PM
Steiner's solution would work even if you had a bigger game. Just set the stagex and shit variables on each frame the boundaries change.

The rooms aren't all going to be square...


The easiest way to to make 4 different walls and put the code on the wall. So if the code on the character is like this

onClipEvent(enterFram){
If(this.hittest(wall1)){
_x = 10
}
}

Change it to smoething like this and put it on the wall

onClipEvent(enterFram){
If(this.hittest(player)){
_x = 10
}
}

I just chose _x = 10 as a random movement code, you can make it whatever you want.

EDIT:

The plus side of putting the hittest on the wall is that you don't have to give instance names to them all, once you make one you can copy and paste the rest. with only having to give one instance name to the player.

I was hoping to avoid that. I'm trying to code most of it from the frame and not use separate code for each object.

But that might be the only way. Thanks.

Kitsune
04-26-2008, 08:56 PM
The rooms aren't all going to be square...


But that might be the only way.

It doesn't matter if they all aren't going to be square. This is where you come in with the programming logic you were CLEARLY given. You program the walls you want in their own codes individually.

The only way? There is no "Only Way" in actionscript, or any other programming for that matter. There is only a vocabulary and a sense of logic that you need.

Bonk
04-26-2008, 09:04 PM
Wait, what?

Explain what Steiner's code does. I though it just made it so you cannot leave the stage.

Kitsune
04-26-2008, 09:21 PM
You set a variable boundry called stagex that doesn't allow you to leave through that variable. If you have like a wall in the middle of a room that goes through the center and reaches to the middle, just make variables for that wall too. xstage and ystage would set the variables to not leave the room (if you can code with logic, cannot leave the room except through a door), but you can make more for more obstacles.

Makes sense?

Bonk
04-26-2008, 10:05 PM
I see. But what if I wanted to make a wall that covered half the screen on one edge?

And this would be hard for a scrolling landscape...

Kitsune
04-26-2008, 10:50 PM
I see. But what if I wanted to make a wall that covered half the screen on one edge?

And this would be hard for a scrolling landscape...
try this shit then. Use a vcam that follows your guy in the code's _x variable assigning, and do this:

if(this.hitTest(_root.wallmc and _x>=_root.wallmc._x)
_x=_root.wallmc._x-2

Left of a jutting wall. Getting it?

Bonk
04-27-2008, 12:44 AM
_root.wallmc

I'm trying to stay away from specific instance names for every single wall and matching code.

But, yeah I've done it so thanks anyway.

Scorpioxxx
04-27-2008, 04:31 AM
Um, I may have fixed your problem. I found some code in an old RPG I half made.


onClipEvent (load) {
speed = 4;
radius = 8;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
if (!_root.wall.hitTest(_x-speed-radius, _y, true)) {
_x -= speed;
}
}
if (Key.isDown(Key.RIGHT)) {
if (!_root.wall.hitTest(_x+speed+radius, _y, true)) {
_x += speed;
}
}
if (Key.isDown(Key.UP)) {
if (!_root.wall.hitTest(_x, _y-speed-radius, true)) {
_y -= speed;
}
}
if (Key.isDown(Key.DOWN)) {
if (!_root.wall.hitTest(_x, _y+speed+radius, true)) {
_y += speed;
}
}
}

It goes in the player and ALL the walls are given the instance 'wall'.

Godfather
04-27-2008, 04:57 AM
Wait, what kind of game is it? Shooting, Adventure, Sports? Tell me and i might be able to help.