As3 How to flip a movieclip to face movement direction? - actionscript-3

Working on a maze game. When the leftkey is pressed the movieclip (char) should turn 90 degrees to the left.
Correct me if i'm wrong but I thought I could use this code;
char.scaleX *= -1;
However, the most important thing is that the character doesnt go through the walls of the maze.
And I think thats my problem for implementing the code above.
Because it doesnt work properly when i put in here;
if(!mazehit) {
char.y += speed;
char.scaleX *= -1;
}
My question to you is, where do I have to put the code to flip the movieclip?
var leftArrow, rightArrow, upArrow, downArrow:Boolean;
var speed:Number = 4;
var charRadius:Number = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
function keyPressed(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.UP) {
upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = true;
}
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.UP) {
upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = false;
}
}
function everyFrame(event:Event):void {
var mazehit:Boolean = false;
if (leftArrow) {
for(var i:int = 0; i < speed; i++) {
if(bounds.hitTestPoint(char.x - charRadius - i, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x -= speed;
}
} else if (rightArrow) {
for(var j:int = 0; j < speed; j++) {
if(bounds.hitTestPoint(char.x + charRadius + j, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x += speed;
}
} else if (upArrow) {
for(var k:int = 0; k < speed; k++) {
if(bounds.hitTestPoint(char.x, char.y - charRadius - k, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.y -= speed;
}
} else if (downArrow) {
for(var m:int = 0; m < speed; m++) {
if(bounds.hitTestPoint(char.x, char.y + charRadius + m, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.y += speed;
}
}
}
thank you for your time

I would update the direction depending on the speed:
char.scaleX = (speed > 0) ? 1 : -1;
Or, by the keys that have been pressed:
if(keyLeft && !keyRight)
{
char.scaleX = -1;
}
else if(keyRight && !keyLeft)
{
char.scaleX = 1;
}
else
{
// keep current direction
}

Related

AS3 Platformer Slope Detection

i am in the process of making a platformer in AS3. However, one thing im struggling with is how to manage slope detection. Preferably id like something similar to that of games like fancy pants adventures, but id really prefer not to use an external thing such as Box2D or CDK, because both of which confuse me greatly, and id generally prefer just to not have one. All my attempts have had the problem that i can get it to work with some slopes but not all (e.g character manages up one slope, but falls through the other) then when i change it to suit the other, it doesnt suit the first one (e.g player manages the second slope, but bounces up and down crazily on the second...
So, any help would be greatly appreciated...
Thanks.
Code is as follows.
//var setup
var leftPressed: Boolean = false;
var rightPressed: Boolean = false;
var upPressed: Boolean = false;
var downPressed: Boolean = false;
var leftBumping: Boolean = false;
var rightBumping: Boolean = false;
var upBumping: Boolean = false;
var downBumping: Boolean = false;
var lowerleftBumping: Boolean = false;
var lowerrightBumping: Boolean = false;
var leftBumpPoint: Point = new Point(-30, -87);
var rightBumpPoint: Point = new Point(30, -87);
var lowerleftBumpPoint: Point = new Point(-32, -2);
var lowerrightBumpPoint: Point = new Point(32, -2);
var upBumpPoint: Point = new Point(0, -174);
var downBumpPoint: Point = new Point(0, 0);
var scrollX: Number = -4;
var scrollY: Number = -68;
var ySpeed: Number = 0;
var xSpeed: Number = 0;
var speedConstant: int = 5;
var maxSpeedConstant: Number = 30;
var frictionConstant: Number = 0.9;
var gravityConstant: Number = 2;
var jumpConstant: Number = -45;
//game
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
function keyDownHandler(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.LEFT) {
leftPressed = true;
} else if (e.keyCode == Keyboard.RIGHT) {
rightPressed = true;
} else if (e.keyCode == Keyboard.UP) {
upPressed = true;
} else if (e.keyCode == Keyboard.DOWN) {
downPressed = true;
}
}
function keyUpHandler(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.LEFT) {
leftPressed = false;
} else if (e.keyCode == Keyboard.RIGHT) {
rightPressed = false;
} else if (e.keyCode == Keyboard.UP) {
upPressed = false;
} else if (e.keyCode == Keyboard.DOWN) {
downPressed = false;
}
}
function loop(e: Event): void {
if (back.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)) {
trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if (back.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)) {
trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if (back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)) {
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if (back.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)) {
trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if (back.hitTestPoint(player.x + lowerleftBumpPoint.x, player.y + lowerleftBumpPoint.y, true)) {
trace("lowerrightBumping");
lowerleftBumping = true;
} else {
lowerleftBumping = false;
}
if (back.hitTestPoint(player.x + lowerrightBumpPoint.x, player.y + lowerrightBumpPoint.y, true)) {
trace("lowerrightBumping");
lowerrightBumping = true;
} else {
lowerrightBumping = false;
}
if (leftPressed) {
xSpeed -= speedConstant;
} else if (rightPressed) {
xSpeed += speedConstant;
}
if (leftBumping) {
if (xSpeed < 0) {
xSpeed *= -0.5;
}
}
if (rightBumping) {
if (xSpeed > 0) {
xSpeed *= -0.5;
}
}
if (upBumping) {
if (ySpeed < 0) {
ySpeed *= -0.5;
}
}
if (downBumping) {
if (ySpeed > 0) {
ySpeed = 0; //set the y speed to zero
}
if (upPressed) {
ySpeed = jumpConstant;
}
} else {
ySpeed += gravityConstant;
}
if (xSpeed > maxSpeedConstant) { //moving right
xSpeed = maxSpeedConstant;
} else if (xSpeed < (maxSpeedConstant * -1)) { //moving left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= frictionConstant;
ySpeed *= frictionConstant;
if (Math.abs(xSpeed) < 0.5) {
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
if (scrollY < -770) {
scrollX = -4;
scrollY = -68;
ySpeed=0
xSpeed=0
}
}
If I was forced to not use the love of my life (Box2D) then I suppose I would play around with hitTestObject and do some math on the angle of any of my slopes to generate the rate at which they would move to start. This question is a bit broad and I think the community here is more for specific problems and not architecture brainstorming.

Coding a simple platformer game and enemies won't show up

I have been having a problem getting enemies to appear and this is the final stretch before I can get the game to fully work.
It was supposed to be a one off, the original plan was to have enemies spawning, but the coordinates I have laid out for the enemies have not worked.
I will attach the main code and the enemy .as file, using actionscript3.
// MAIN TIME LINE CODE
import flash.events.Event;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
//buttong pressing
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
// collision variables
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
// character collision
var gravityConstant:Number = 1.5;
var jumpConstant:Number = - 35;
var maxSpeedConstant:Number = 18;
//jumping
var animationState:String = "idle";
// for the animation
var bulletList:Array = new Array();
var enemyList:Array = new Array();
var currentLevel:int = 1;
var scrollX:Number = 0;
var scrollY:Number = 500;
//parralax
var xSpeed:Number = 0;
var ySpeed:Number = 0;
// speeeeeed
var speedConstant:int = 5;
var friction:Number = 0.9;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
addEnemiesToLevel1();
function addEnemiesToLevel1():void {
addEnemy(340, 390);
/* addEnemy(900, -490);
addEnemy(2005, -115);
addEnemy(1225, -875);*/
//adds an enemy
}
function loop(e:Event):void{
if(back.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if(back.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant;
/*player.x -= xSpeed;*/
player.scaleX = -1;
trace("fff");
} else if(rightPressed) {
xSpeed += speedConstant;
/*player.x += xSpeed;*/
player.scaleX = 1;
// player movement left and right
}
/*player.scaleX = 1;*/ // copy in case
/*if(upPressed){
ySpeed -= speedConstant;
} else if(downPressed){
ySpeed += speedConstant;
}*/
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
} ////BUMPING
if(downBumping){ //if touching the floor
if(ySpeed > 0){
ySpeed = 0;
trace("1")
}
if(upPressed) { //y speed to jump constant
ySpeed = jumpConstant;
trace("2");
}
} else { // if not touching the floor
ySpeed += gravityConstant; // moves downward
trace("portobello");
}
if(xSpeed > maxSpeedConstant) { // lettin you move right
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)) { // leting you move left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= friction;
ySpeed *= friction;
if(Math.abs(xSpeed) < 0.5) {
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping) {
animationState = "running";
} else if(downBumping) {
animationState = "idle";
} else {
animationState = "jumping";
}
if (player.currentLabel != animationState) {
player.gotoAndStop(animationState);
}
}
if (player.currentLabel != animationState) {
player.gotoAndStop(animationState);
}
if (enemyList.length > 0) // if there are any enemies left in the enemyList
{
for (var i:int = 0; i < enemyList.length; i++) // for each enemy in the enemyList
{
if (bulletList.length > 0) // if there are any bullets alive
{
for (var j:int = 0; j < bulletList.length; j++) // for each bullet in the bulletList
{
if ( enemyList[i].hitTestObject(bulletList[j]) )
{
trace("Bullet and Enemy are colliding");
enemyList[i].removeSelf();
bulletList[j].removeSelf();
}
}
}
}
}
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if(e.keyCode == Keyboard.UP){
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
}
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
if(e.keyCode == Keyboard.SPACE) {
fireBullet();
}
}
function addEnemy(xLocation:int, yLocation:int): void {
var enemy:Enemy = new Enemy(xLocation, yLocation);
back.addChild(enemy);
enemy.addEventListener(Event.REMOVED, enemyRemoved);
enemyList.push(enemy);
}
function enemyRemoved(e:Event):void {
e.currentTarget.removeEventListener(Event.REMOVED, enemyRemoved);
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
function fireBullet():void {
var playerDirection:String;
if(player.scaleX < 0) {
playerDirection = "left";
} else if(player.scaleX > 0) {
playerDirection = "right";
}
var bullet:Bullet = new Bullet(player.x, player.y, playerDirection);
stage.addChild(bullet);
// firing direction
}
function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED, bulletRemoved); // stops and error from occuring in the event listener
bulletList.splice(bulletList.indexOf(e.currentTarget), 1); // removes a bullet from the list
}
The enemy .as file:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
public function Enemy(xLocation:int, yLocation:int) {
// constructor code
x = xLocation;
y = yLocation;
addEventListener(Event.ENTER_FRAME, loop);
}
public function loop(e:Event):void {
//looping code
}
public function removeSelf():void {
trace("remove self");
removeEventListener(Event.ENTER_FRAME, loop); //stops the loop
this.parent.removeChild(this); // tell this objects "parent object" to remove said object
}
}
}

Game Rotation Collision Problems

Im attempting to make a basic maze game with a slight twist. At set intervals (set by a timer event) the maze rotates 90%. The problem I'm having is in regards to the hitTestPoint. The hit test works prior to the maze roataion and works after a full 360% rotation but stops working during the 90%, 180% and 270% rotation points. I have exhausted all my knowledge (As limited as a 5 month AS3 programmer) in AS3 to resolve this problem and am at my wits end.
The maze is in a container and the container is rotating, this is so that the maze in effect has a secondary moving pivot point that continually follows the player around tha maze. additionally the container is moving on key press not the player.
Could anyone please halp me by explaining what is causing the problem, how I can fix it and if possible show me an example of the code I should be using.
Here is what I have so far.
stop();
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.sensors.Accelerometer;
var speed:Number = 5;
var northSpeed = speed;
var southSpeed = speed;
var eastSpeed = speed;
var westSpeed = speed;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var Orientation:int = 0;
var count:int = 0;
var timer:Timer = new Timer(1000,60);
var time = 60;
player.addEventListener(Event.ENTER_FRAME, MovePlayer);
containBox.maze.addEventListener(Event.ENTER_FRAME, hitWalls);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyDepressed);
containBox.maze.addEventListener(Event.ENTER_FRAME, spin);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerHandle);
function timerHandle(e:TimerEvent):void
{
txt_time.text = time;
time--;
}
function hitWalls(event:Event):void
{
if (upPressed==true && containBox.maze.hitTestPoint(player.x,player.y,true))
{
northSpeed = 0;
player.y = player.y+=2;
}
else
{
northSpeed = speed;
}
}
function spin(event:Event):void
{
if (time <= 0)
{
txt_time.text = "TIMES UP!";
}
if (time <= 54)
{
containBox.rotation = 90;
Orientation = 1;
}
if (time <= 50)
{
containBox.rotation = 180;
Orientation = 2;
}
if (time <= 48)
{
containBox.rotation = 270;
Orientation = 3;
}
if (time <= 46)
{
containBox.rotation = 0;
Orientation = 0;
}
}
function MovePlayer(event:Event):void
{
if (Orientation == 0)
{
if (upPressed)
{
containBox.maze.y += northSpeed;
}
if (downPressed)
{
containBox.maze.y -= southSpeed;
}
if (leftPressed)
{
containBox.maze.x += westSpeed;
}
if (rightPressed)
{
containBox.maze.x -= eastSpeed;
}
}
else if (Orientation == 1)
{
if (upPressed)
{
containBox.maze.x += 5;
}
if (downPressed)
{
containBox.maze.x -= 5;
}
if (leftPressed)
{
containBox.maze.y -= 5;
}
if (rightPressed)
{
containBox.maze.y += 5;
}
}
else if (Orientation == 2)
{
if (upPressed)
{
containBox.maze.y -= 5;
}
if (downPressed)
{
containBox.maze.y += 5;
}
if (leftPressed)
{
containBox.maze.x -= 5;
}
if (rightPressed)
{
containBox.maze.x += 5;
}
}
else if (Orientation == 3)
{
if (upPressed)
{
containBox.maze.y += 5;
}
if (downPressed)
{
containBox.maze.y -= 5;
}
if (leftPressed)
{
containBox.maze.x += 5;
}
if (rightPressed)
{
containBox.maze.x -= 5;
}
}
}
function KeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = true;
break;
};
case Keyboard.DOWN :
{
downPressed = true;
break;
};
case Keyboard.LEFT :
{
leftPressed = true;
break;
};
case Keyboard.RIGHT :
{
rightPressed = true;
break;
}
}
}
function KeyDepressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = false;
break;
};
case Keyboard.DOWN :
{
downPressed = false;
break;
};
case Keyboard.LEFT :
{
leftPressed = false;
break;
};
case Keyboard.RIGHT :
{
rightPressed = false;
break;
}
}
}
Many thanks,
Reece.
You need to do 2D coordinate system transformation to do a proper comparison. See, the player basically stands on a fixed point, thus (player.x, player.y) is fixed, and hitTestPoint() uses the coordinates supplied as local to the this object, in your case the maze. To get the player's coordinates in the maze's system, you need first get global position, then derive local position, there are functions for that, localToGlobal() and globalToLocal() respectively.
function hitWalls(event:Event):void
{
var p:Point=new Point(player.x,player.y);
var dp:Point=containBox.maze.globalToLocal(player.parent.localToGlobal(p));
// now test vs dp instead of player.
if (upPressed==true && containBox.maze.hitTestPoint(dp.x,dp.y,true))
{
northSpeed = 0;
player.y = player.y+=2;
}
else
{
northSpeed = speed;
}
}

Cannot access a property or method of a null object reference. -enterframe.Trying to get palayer out of maze

i'm making a simple game and i'm still learning as3 i want to hit test object on the stage , player with box if hittestobject gotoframe or scene all the objects are on the stage with corect instancename, maintimeline as3 below everything works fine but i get the null error.
it is a maze game.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.ui.Keyboard;
var rightArrow:Boolean = false;
var leftArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var speed:int = 20;
stage.addEventListener(KeyboardEvent.KEY_DOWN, stage_onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, stage_onKeyUp);
stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
function stage_onKeyDown(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = true;
if(event.keyCode == Keyboard.LEFT) leftArrow = true;
if(event.keyCode == Keyboard.UP) upArrow = true;
if(event.keyCode == Keyboard.DOWN) downArrow = true;
}
function stage_onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = false;
if(event.keyCode == Keyboard.LEFT) leftArrow = false;
if(event.keyCode == Keyboard.UP) upArrow = false;
if(event.keyCode == Keyboard.DOWN) downArrow = false;
}
function stage_onEnterFrame(event:Event):void {
var rect:Rectangle = player.getBounds(this);
var i:int = 0;
var xBump:int = 0;
var yBump:int = 0;
if(rightArrow) {
xBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.right + i, player.y, true)) {
xBump = i - 1;
break;
}
}
}
if(player.hitTestObject(box))
{
// Go to next scene
nextFrame();
}
if(leftArrow) {
xBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.left - i, player.y, true)) {
xBump = -i + 1;
break;
}
}
}
if(upArrow) {
yBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(player.x, rect.top - i, true)) {
yBump = -i + 1;
break;
}
}
}
if(downArrow) {
yBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(player.x, rect.bottom + i, true)) {
yBump = i - 1;
break;
}
}
}
player.x += xBump;
player.y += yBump;
}

Collision and movements not working as expected

I am trying to do an example of collision in Action Script 3. It's a character that should stop when it hits a platform. It works well when I move only to the right, left, up or down directions, but if I try to move in the diagonals, if the characteris colliding with the platform, the object goes to a different area of the screen.
This is the compiled example: http://dl.dropbox.com/u/5282142/GameDemo.html
And below is my code.
Now, does anyone know a better way to do what I am doing, or how can I get the character not to go to a weird position when I try to move it in a diagonal?
var level:Array = new Array();
for (var i = 0; i < numChildren; i++) {
if (getChildAt(i) is Platform) {
level.push(getChildAt(i).getBounds(this));
}
}
var speedX:int = 0;
var speedY:int = 0;
var kLeft:Boolean = false;
var kRight:Boolean = false;
var kDown:Boolean = false;
var kUp:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
function onKeyDownHandler(event:KeyboardEvent):void {
if (event.keyCode == 37) kLeft = true;
if (event.keyCode == 38) kUp = true;
if (event.keyCode == 39) kRight = true;
if (event.keyCode == 40) kDown = true;
}
function onKeyUpHandler(event:KeyboardEvent):void {
if (event.keyCode == 37) kLeft = false;
if (event.keyCode == 38) kUp = false;
if (event.keyCode == 39) kRight = false;
if (event.keyCode == 40) kDown = false;
}
addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void {
moveChar();
bound();
}
function moveChar():void {
if (kLeft) {
speedX = -10;
} else if (kRight) {
speedX = 10;
} else {
speedX *= 0.5;
}
if (kUp) {
speedY = -10;
} else if (kDown) {
speedY = 10;
} else {
speedY *= 0.5;
}
character.x += speedX;
character.y += speedY;
}
function bound():void {
if (character.x > (800 - character.width/2)){
character.x = 800 - character.width/2;
}
if (character.x < (character.width/2)){
character.x = character.width/2;
}
if (character.y > (480 - character.height/2)){
character.y = 480 - character.height/2;
}
if (character.y < (character.height/2)){
character.y = character.height/2;
}
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedX > 0) {
character.x = level[i].left - character.width/2;
}
if (speedX < 0) {
character.x = level[i].right + character.width/2;
}
}
}
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedY > 0) {
character.y = level[i].top - character.height/2;
}
if (speedY < 0) {
character.y = level[i].bottom + character.height/2;
}
}
}
}
Your Problem lies in this part of your code :
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedX > 0) {
character.x = level[i].left - character.width/2;
}
if (speedX < 0) {
character.x = level[i].right + character.width/2;
}
}
}
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedY > 0) {
character.y = level[i].top - character.height/2;
}
if (speedY < 0) {
character.y = level[i].bottom + character.height/2;
}
}
}
The character does not actually move to a random position. It is being set by you, above.
You have to create a better logic (as per the scenario) where your code should not get confused when two keys are down simultaneously & a collision occurs.