*Posted this on another forum and got no response, so I'm just going to copy paste it here. *
I've actually been looking for a proper way to do this for a really really long time, and I've only come close once.
Basically, I'm trying to make a game with a character that moves upwards/downwards with the angle of the ground he's walking on. (He doesn't have to rotate)
Sounds simple enough. I can't even tell you how many different approaches I've taken. In both AS2 AND AS3.
I'll attach an fla of where I've come close. What I can't figure out is how to make it precise (as in no rumbling while walking up, and no falling when walking down)
A bonus would be: Sliding down if the downward slope is great enough.
Here's the code in P1:
ActionScript Code:
onClipEvent (load) {
var jumping:Boolean = true;
var jump:Number = 0;
var wallleft:Object = {x:350, y:200};
var wallright:Object = {x:375, y:200};
var slideleft:Object = {x:350, y:235};
var slideright:Object = {x:375, y:235};
var catcher:Object = {x:362.5, y:245};
var charpoint:Object = {x:362.5, y:250};
}
onClipEvent (enterFrame) {
_root.ground._y += jump;
if (jump<=-10) {
jump = -9;
}
if (_root.ground.hitTest(charpoint.x, charpoint.y, true)) {
jump = 0;
jumping = false;
_root.ground._y -= 5;
} else {
jumping = true;
}
if (_root.ground.hitTest(catcher.x, catcher.y, true)) {
_root.ground._y += 5;
}
if (_root.ground.hitTest(wallleft.x, wallleft.y, true)) {
_root.ground._x -= 5;
} else {
if (_root.ground.hitTest(slideleft.x, slideleft.y, true) && jumping == false) {
if (Key.isDown(Key.LEFT)) {
_root.ground._y += 5;
}
}
}
if (_root.ground.hitTest(wallright.x, wallright.y, true)) {
_root.ground._x += 5;
} else {
if (_root.ground.hitTest(slideright.x, slideright.y, true) && jumping == false) {
if (Key.isDown(Key.RIGHT)) {
_root.game.stage._y += 5;
}
}
}
if (_root.roof.hitTest(wallleft.x, wallleft.y, true)) {
jump = -5;
}
if (_root.roof.hitTest(wallright.x, wallright.y, true)) {
jump = -5;
}
if (Key.isDown(Key.SPACE) && jumping == false) {
jumping = true;
jump += 15;
}
if (jumping == true) {
jump -= 1;
}
if (Key.isDown(Key.LEFT)) {
_root.ground._x += 5;
}
if (Key.isDown(Key.RIGHT)) {
_root.ground._x -= 5;
}
}
*Then after this, I tried yet another code, which almost worked.*
Okay, I've taken a different approach this time. I found a small tutorial and made some simpler code from that.
It uses a "while" loop, which I honestly have no idea how to use. I had the loop push the character up and it worked perfectly, but I want the character to stay put in the center of the screen, and the stage to move behind the character. When I changed the code, the "while" loop would continue until the character was all the way to the top of the bounding box of the stage.
I'm pretty sure it's because the loop is using the entire bounding box of the stage, but I'm not sure why.
Here's the code:
ActionScript Code:
onClipEvent (load) {
speed = 7;
jumping = false;
jump = 0;
var p1jumppoint:Object = {x:350, y:176};
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
_root.ground._x -= speed;
}
if (Key.isDown(Key.LEFT)) {
_root.ground._x += speed;
}
if (Key.isDown(Key.SPACE) && !jumping) {
jump = -12;
jumping = true;
}
if (_root.ground.hitTest(_x, _y, true)) {
_root.test1.gotoAndStop(2);
jumping = false;
jump = 0;
} else {
_root.test1.gotoAndStop(1);
}
if (!_root.ground.hitTest(_x, _y, true)) {
_root.test2.gotoAndStop(2);
jump += 1;
_root.ground._y -= jump;
} else {
_root.test2.gotoAndStop(1);
}
if (jump>=11) {
jump = 10;
}
while (_root.ground.hitTest(_x, _y, true)) {
jumping = false;
_root.ground._y++;
}
}
I'll attach an fla with this code in it, since it seems like I almost have it working.