ActionScripted Barriers?
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
Is there any code that I can put on a movie clip, so that no matter what direction the character hits it at, it will deflect him in the exact opposite direction? It would incredibly helpful.
- LittleGirls
-
LittleGirls
- Member since: Feb. 20, 2009
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
At 2/21/09 11:03 PM, TheGoldenMonk wrote: Is there any code that I can put on a movie clip, so that no matter what direction the character hits it at, it will deflect him in the exact opposite direction? It would incredibly helpful.
Something like this?
addEventListener( Event.ENTER_FRAME, updatePlayer );
function updatePlayer( event:Event ):void
{
x += vX;
y += vY;
if ( hitTestObject( barrier ) )
{
vX = -vX;
vY = -vY;
}
return;
}
Notes:
1) Written in AS3 because you didn't specify a version of AS.
2) This goes on the player/character/whatever and not the barrier. Main reason for this is the fact that you're updating the player each frame anyway, and adding "enter frame" update functions all over the place to handle stuff like this is bad programming IMO. If you need multiple barriers, then loop thru them all in the main update function of your game.
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
Oh sorry, I needed AS2. I appreciate the response though.
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 2/22/09 01:54 PM, TheGoldenMonk wrote: Oh sorry, I needed AS2. I appreciate the response though.
Converted to AS2, kind of:
player.onEnterFrame=function{
this.x += this.vX;
this.y += this.vY;
if ( this.hitTest(_root.barrier ) ){
this.vX*=-1;
this.vY*=-1;
}
}
vX is horizontal speed, vY is vertical speed. Ideally though you'll need separate if{} sections to handle bounce in each direction, as that will reverse both vX and vY - you'll probably be wanting to reverse vY only if hitting top/bottom, and vX when hitting left/right.
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
Thanks for the conversion Denvish. I appreciate it.
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
At 2/22/09 04:12 PM, Denvish wrote:At 2/22/09 01:54 PM, TheGoldenMonk wrote: Oh sorry, I needed AS2. I appreciate the response though.Converted to AS2, kind of:
player.onEnterFrame=function{
this.x += this.vX;
this.y += this.vY;
if ( this.hitTest(_root.barrier ) ){
this.vX*=-1;
this.vY*=-1;
}
}
vX is horizontal speed, vY is vertical speed. Ideally though you'll need separate if{} sections to handle bounce in each direction, as that will reverse both vX and vY - you'll probably be wanting to reverse vY only if hitting top/bottom, and vX when hitting left/right.
Sorry to double post, but the code doesn't necessarily do anything at all. I have all the instance names in place, and I'm not a code newbie, so I know what to look for if something doesn't work, but I'm not exactly sure why it isn't working. Is there something else I should be doing? Maybe a variable I was supposed to put in? Any help would be greatly appreciated.
- Johnny
-
Johnny
- Member since: Apr. 17, 2004
- Offline.
-
- Forum Stats
- Member
- Level 24
- Blank Slate
At 2/23/09 12:13 AM, TheGoldenMonk wrote: Sorry to double post, but the code doesn't necessarily do anything at all.
For AS2.0
this.x += this.vX
Has to be:
this._x += this.vX;
Perpetually looking for time to return to the arts.
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
:At 2/23/09 12:16 AM, Johnny wrote:
For AS2.0
this.x += this.vX
Has to be:
this._x += this.vX;
Yeah, I tried that. Not sure whats wrong. I included a picture to put things in perspective. The block is the barrier. Both the character and the block are on the same layer, and the code is on the character as well.
- LittleGirls
-
LittleGirls
- Member since: Feb. 20, 2009
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
At 2/23/09 12:54 AM, TheGoldenMonk wrote: Yeah, I tried that. Not sure whats wrong. I included a picture to put things in perspective. The block is the barrier. Both the character and the block are on the same layer, and the code is on the character as well.
So nothing happens at all? Where are you putting the definition for the onEnterFrame function?
Run some traces to see what is up. Put a trace in the onEnterFrame function. Is it even getting called? Try tracing "this.vX"; is it defined?
- Johnny
-
Johnny
- Member since: Apr. 17, 2004
- Offline.
-
- Forum Stats
- Member
- Level 24
- Blank Slate
Try this. Put this on the first frame in the main timeline, and give your character an instance name of "player" and the barrier "barrier"
var player.vX:Number = 1; //arbitrary number.
var player.vY:Number = 1; //arbitrary number.
onEnterFrame=function{
player._x += player.vX; //will need to put this line and the next in a Key statement, for control
player._y += player.vY;
if ( player.hitTest(barrier ) ){
player.vX*=-1;
player.vY*=-1;
}
}
The code isn't complete, but see what happens if you put that in the main timeline with the proper instance names.
Perpetually looking for time to return to the arts.
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
At 2/23/09 04:48 AM, LittleGirls wrote: Run some traces to see what is up. Put a trace in the onEnterFrame function. Is it even getting called? Try tracing "this.vX"; is it defined?
Ok. I reformatted the code Denvish posted, and added some traces to make sure it was registering the hitTest, and the value of the X and Y speed variables.
The Barrier works, but now I have a new problem. When you hit the barrier with the character, it bounces into the block slightly, and you can get stuck inside of it.
Example attached: http://spamtheweb.com/ul/upload/240209/2 180_Basic_Controls_Main.php?lightbox
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 2/23/09 12:16 AM, Johnny wrote: Has to be:
this._x += this.vX;
Doh, can't believe I didn't notice that.
At 2/24/09 12:37 AM, TheGoldenMonk wrote: The Barrier works, but now I have a new problem. When you hit the barrier with the character, it bounces into the block slightly, and you can get stuck inside of it.
Make sure your character is centred in the MC (or create a smaller internal MC to run the hitTest on).
Also, add another player._x+=player.vX; player._y+=player.vY; directly after the vX/vY reversal on detecting hitTest as true, ie:
player.onEnterFrame=function{
this._x += this.vX;
this._y += this.vY;
if ( this.hitTest(_root.barrier ) ){
this.vX*=-1;
this.vY*=-1;
this._x += this.vX;
this._y += this.vY;
}
} - TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
What exactly is vX and vY, because flash isn't recognizing it at all. Is it a variable I have to set up? or a special one, like "i" ?
- Doomsday-One
-
Doomsday-One
- Member since: Oct. 28, 2005
- Offline.
-
- Forum Stats
- Member
- Level 10
- Programmer
At 2/24/09 04:59 PM, TheGoldenMonk wrote: What exactly is vX and vY, because flash isn't recognizing it at all. Is it a variable I have to set up? or a special one, like "i" ?
vX and vY are regular number variables. The codes already posted should define it without you having to set it up.
Oh, and 'i' is also a regular variable; it's just that people like using that letter =)
Doomsday-One, working on stuff better than preloaders. Marginally.
- Johnny
-
Johnny
- Member since: Apr. 17, 2004
- Offline.
-
- Forum Stats
- Member
- Level 24
- Blank Slate
At 2/24/09 04:59 PM, TheGoldenMonk wrote: What exactly is vX and vY, because flash isn't recognizing it at all. Is it a variable I have to set up? or a special one, like "i" ?
I defined it in the code I wrote out for you.
Perpetually looking for time to return to the arts.
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 2/24/09 04:59 PM, TheGoldenMonk wrote: What exactly is vX and vY, because flash isn't recognizing it at all. Is it a variable I have to set up? or a special one, like "i" ?
Generally vX/vY or xvel/yvel are used for horizontal (_x axis) and vertical (_y axis) velocity. In your case, vX is the walking speed and vY is the jumping/falling speed
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
The code ends up with syntax errors, and I can't figure out where.
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 2/24/09 06:35 PM, TheGoldenMonk wrote: The code ends up with syntax errors, and I can't figure out where.
Would help if you posted the code you're currently using, and the syntax errors
- TheGoldenMonk
-
TheGoldenMonk
- Member since: Jan. 31, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
The code he posted had syntax errors. My code is basically that, but on the player MC, and its adds a negative version of an xSpeed variable to the players position on the X axis when it hits the block.



