PDA

View Full Version : [AS2] Platform Game - Code error


BMGP
10-31-2010, 08:35 PM
Hi!
I have a little error at coding a platform game.
How it is built up:
Scene1
______Preloader
______Intro
______Menu
______Game
____________character_mc(Code)
____________ground_mc

Code on character_mc:
onClipEvent (load) {
var ground:MovieClip = _root.ground;
var grav:Number = 0;
var gravity:Number = 2;
var speed:Number = 7;
var maxJump:Number = -12;
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;
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
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;
}
}


When i test it my charcter just falls throw the ground. But when i made the character and the ground in a new project on the main timeline(scene 1) it works...
So the code must be changed but i dont know exactly what... Something with _root etc. or???

When i change "var ground:MovieClip = _root.ground;" to "var ground:MovieClip = _root.game_mc.ground;" it doesnt work.

PLEASE HELP!!!!!
BMGP

bluemagica
11-01-2010, 05:21 PM
if your ground has a instance name "ground" then you can try

var ground:Movieclip = _parent.ground;

if it doesn't find ground, then there can be several reasons including your onload being fired before the ground is created....just for a test, try accessing the player from the ground mc.

BMGP
11-06-2010, 11:30 AM
just for a test, try accessing the player from the ground mc.

e.g. When i click on ground it should eg move the player!?


EDIT:
"onload being fired before the ground is created"
i have a preloader...