Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

ActionScript 2.0

Does anyone know action script 2.0? If you do, I need some quick URGENT help.

8 Answers

Sean T. Unwin
Sean T. Unwin
28,660 Points

I am probably a bit rusty, but I used to do some AS coding. I'll do what I can to help.

Rodrigo Chousal
Rodrigo Chousal
16,009 Points
onClipEvent (load) {
var ground:MovieClip = _root.ground;
var grav:Number = 0;
var gravity:Number = 2;
var speed:Number = 14;
var maxJump:Number = -26
;
var touchingGround:Boolean = false;
}
onClipEvent (enterFrame) {
_y += grav;
grav += gravity;
while (ground.hitTest(_x, _y, true)) {
_y -= gravity;
grav = 0;
}
if (ground.hitTest(_x, _y+5, true)) {
touchingGround = true;
} else {
touchingGround = false;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
_xscale = 100
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
_xscale = -100
}
if (Key.isDown(Key.UP) && touchingGround) {
grav = maxJump;
}
if (ground.hitTest(_x+(_width/2), _y-(_height/2), true)) {
_x -= speed;
}
if (ground.hitTest(_x-(_width/2), _y-(_height/2), true)) {
_x += speed;
}
if (ground.hitTest(_x, _y-(height), true)) {
grav = 3;
}
}


onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
this.gotoAndStop("2");
} else {

if (Key.isDown(Key.RIGHT)) {
this.gotoAndStop("2");
} else {
this.gotoAndStop("1");
}


}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
this.gotoAndStop("3");
}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.DOWN)) {
this.gotoAndStop("4");
}
}
Rodrigo Chousal
Rodrigo Chousal
16,009 Points

Okay, so this is a platform game. Our code for our character is the one above.

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

The game consists on touching bubbles that get you to the next level. When you grab the incorrect bubble, you get sent to another keyframe which is a game over screen. Each level is in a different keyframe. Here is the code for the bubble:

Rodrigo Chousal
Rodrigo Chousal
16,009 Points
onClipEvent (enterFrame) {
if (this.hitTest(_root.char))
{
_root.gotoAndStop(150);
}
}
Rodrigo Chousal
Rodrigo Chousal
16,009 Points
onClipEvent (enterFrame) {
if (this.hitTest(_root.char))
{
_root.gotoAndStop(132);
}
}
Sean T. Unwin
Sean T. Unwin
28,660 Points

What is the issue? Is it not going to the correct keyframe on the hitTests? I assume the last 2 onClipEvent's you posted are attached to good and bad bubbles?

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

Yes, the first onClipEvent I posted is the wrong bubble, the second is the correct bubble. When the character hits any of the two bubbles, it goes right back to the first frame and stops.

Sean T. Unwin
Sean T. Unwin
28,660 Points

Are you sure the frames (150 and 132) that you want to target are in _root or somewhere else in the _level hierarchy? It's possible you have a scoping issue depending upon the context of _root if you have multiple .swf's or _level's.

From what I can recall, it is advisable to use the format this._parent or _parent instead of _root to avoid scoping issues with portability and loading multiple .swf's.

edit: Refined post after shaking the dust off and thinking about AS again. ;-p

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

That did not work... Is there a way for me to send you my .fla so that you can proofread it? Maybe your email?

Sean T. Unwin
Sean T. Unwin
28,660 Points

You can connect with me on Google+ and put your .fla on Google Drive for me to download, if you use that.

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

I just did, I sent you a message.