Hi I am trying to make a game with gravity flipping as a mechanic much like the old miniclip game gravity guy but i cant seem to make it work. please help
This is the theory I have in my head. I have a movieclip and its always falling/this.y + dy. But once it collides with the platform dy will be set to 0 so it will stop falling. Then when i press the space bar, dy will be set to 3 again but it will multiply by -1 so it will start flying upwards instead.
This is what i have so far
package {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
public class Player extends MovieClip{
//Properties
private var moveLeft:Boolean;
private var moveRight:Boolean;
private var gravity:Boolean; //Switches gravity
private var dx:Number //Speed of player moving on X axis
private var dy:Number //Speed of player moving on Y axis
private var level:MovieClip;
private var platforms:Array;
private var tilesTop:Array;
private var tilesBtm:Array;
private var tilesLeft:Array;
private var tilesRight:Array;
private var grounded:Boolean;
private var flipGravity:Number;
//Constructor
public function Player(iMain:Main) {
level = iMain.getLevel();
platforms = level.getPlatforms();
tilesTop = level.getTilesTop();
tilesBtm = level.getTilesBtm();
tilesLeft = level.getTilesLeft();
tilesRight = level.getTilesRight();
dx = 10;
dy = 10;
flipGravity = -1;
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
//Methods
// Method: Add to stage handler
private function addedToStageHandler(evt:Event):void {
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
this.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
//Methods: Key down handler
private function keyDownHandler(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.LEFT) {
moveLeft = true;
}
else if (evt.keyCode == Keyboard.RIGHT) {
moveRight = true;
}
else if (evt.keyCode == Keyboard.SPACE) {
if (gravity) {
gravity = false;
}
else if (!gravity) {
gravity = true;
}
}
}
//Methods: Key up handler
private function keyUpHandler(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.LEFT) {
moveLeft = false;
}
else if (evt.keyCode == Keyboard.RIGHT) {
moveRight = false;
}
}
//Methods: Move player
public function movePlayer() {
// Move downward
this.y += dy;
grounded = false;
// *** Player's collision tests
// Check if it lands on platform
landOnPlatform();
// Move left
if (moveLeft) { // equivalent to if (moveLeft == true)
if (this.x > 200) { //move player
this.x -= dx;
}
else {
level.moveLevel(dx);
if ( level.x >= 0 ) {//stage stop moving so that the player won't fall of the screen(left)
level.x = 0;
this.x -= dx;
if (this.x < this.width/2) {
this.x=this.width/2;
}
}
}
}
// Move right
if (moveRight) {
if (this.x < 600) { //move player
this.x += dx;
}
else {
level.moveLevel(-dx);
if (level.x <= -1600) { //stage stop moving so that the player won't fall of the screen(right)
level.x = -1600;
this.x += dx;
if (this.x > stage.stageWidth - this.width/2) {
this.x= stage.stageWidth - this.width/2;
}
}
}
}
//Gravity flip
if (!gravity) {
dy *= flipGravity;
grounded = false;
}
}
private function landOnPlatform() {
for (var i:int=0;i<platforms.length;i++) {
/*if (this.hitTestObject(platforms[i])) {
dy = 0;
grounded = true;
this.y = platforms[i].y;
break;
}*/
if (this.hitTestObject(tilesTop[i])) {
dy = 0;
grounded = true;
this.y = tilesTop[i].y;
break;
}
else if (this.hitTestObject(tilesBtm[i])) {
dy = 0;
grounded = true;
this.y = tilesBtm[i].y;
break;
}
else if (this.hitTestObject(tilesLeft[i])) {
dy = 0;
grounded = true;
this.x = tilesLeft[i].x;
break;
}
else if (this.hitTestObject(tilesRight[i])) {
dy = 0;
grounded = true;
this.x = tilesRight[i].x;
break;
}
}
}
}
}
This is the player class above
I have a gameloop running in the main class and its calling the function of move player.
So when gravity is false, the player will float up and when gravity is true the player will drop down. I wrote the code for when dy multiplies with -1 already but when i press space nothing seems to happen
This is all the files i have. Link