Home Tutorials Forums Articles Blogs Movies Library Employment Press
Prev Previous Post   Next Post Next
Old 04-14-2012, 04:26 PM   #1
heavyartilary117
Registered User
 
Join Date: Apr 2012
Posts: 1
Default [AS3] game acting glitchy

Hi im just trying to make a simple platform game but the character keeps bouncing off the floor like its glitching, i can still move and jump he just keeps bouncing, and wen i go up to walls he go to the top of them how can i fix that, heres the code

import flash.events.KeyboardEvent;
import flash.events.Event;

//some variables to track the players speed

var speedX=0;
var speedY=0;

// loop through all platform objects to generate a level

var level:Array = new Array();
for (var i=0; i<numChildren; i++) {
if (getChildAt(i) is Platform) {
level.push(getChildAt(i).getRect(this));
}
}

// make variables to store key states

var kUp = false;
var kDown = false;
var kLeft = false;
var kRight = false;

//listen for key presses and release

stage.addEventListener(KeyboardEvent.KEY_DOWN, kD);
stage.addEventListener(KeyboardEvent.KEY_UP, kU);

function kD(k:KeyboardEvent) {
if (k.keyCode==37) kLeft=true;
if (k.keyCode==38) kUp=true;
if (k.keyCode==39) kRight=true;
if (k.keyCode==40) kDown=true;
}
function kU(k:KeyboardEvent) {
if (k.keyCode==37) kLeft=false;
if (k.keyCode==38) kUp=false;
if (k.keyCode==39) kRight=false;
if (k.keyCode==40) kDown=false;
}

// Make a looping function

addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event) {
player.x+=speedX;
player.y+=speedY;

// Horizontal movement

if (kLeft) {
speedX=-10;
//player.gotoAndStop("run");
player.scaleX=player.scaleY*-1;
} else if (kRight) {
speedX=10;
player.scaleX=player.scaleY;
//player.gotoAndStop("run");
} else {
speedX*=0.5; // friction
//player.gotoAndStop("idle");
}
player.x+=speedX;

//Horizontal hit tests

for (i=0; i<level.length; i++) {
if (player.getRect(this).intersects(level[i])) {
if (speedX > 0) { //moving right
player.x = level[i].left-player.width/2;
}
}
if (player.getRect(this).intersects(level[i])) {
if (speedX < 0) { //moving left
player.x = level[i].right+player.width/2;
}
}
speedX=0; // kill the speed
}

//Vertical checks

// Gravity

speedY+=1;
player.y+=speedY;
var jumpable=false;

//hit tests
for (i=0; i<level.length; i++) {
if (player.getRect(this).intersects(level[i])) {
if (speedY > 0) { //moving down
player.y = level[i].top-player.height/2;
speedY=0;
jumpable=true;
}
if (speedY < 0) { //moving up
player.y = level[i].bottom+player.height/2;
speedY=0;
}
speedY=0; // kill the speed
}
}

// jump if possible

if (kUp && jumpable) {
speedY=-20; // jump height
}

// move the camera with the player

this.x=-player.x+(stage.stageWidth/2); // center the player
this.y=-player.y+(stage.stageHeight/2);

//if (speedX >-0.5 && speedX < 0.5) {
//player.gotoAndStop("idle");
//} else {
//player.gotoAndStop("run");
//}

}
heavyartilary117 is offline   Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:52 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.