Game Rotation Collision Problems - actionscript-3

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;
}
}

Related

Fishing Game Code ActionScript 2.0 to ActionScript 3.0

I am trying to make a fishing game where each fish you catch gives you a point and different enemies subtract points. I have found a tutorial for this in ActionScript 2 but I can't find any for action script 3 (here is the link to what I want to replicate: http://www.flashkit.com/tutorials/Games/Creating-Imtiazur-1297/index.php). I am relatively new at action script as I have only made animations before but I was able to do a little but then couldn't figure out the rest.
I currently have the fishing line moving up and down and does not go beyond the fishing line but I'm not sure how to make the line get longer when it goes down. At the moment the whole line and hook move with a space between it and the rod. I also have to click before the line moves (I'm not sure how to make it instantly usable when going to the frame). I also have one fish moving across the screen. I don't know how to make multiple of the same fish or enemies going across the screen at the same time.
Any help would be greatly appreciated​!
fish.addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
function fl_AnimateHorizontally(event:Event)
{
fish.x = fish.x + 5; if (fish.x>1100)
{
fish.x = -100;
}
shark.x = shark.x + 7; if (shark.x>3500)
{
shark.x = -400;
}
jellyfish.x = jellyfish.x + 3; if (jellyfish.x>3000)
{
jellyfish.x = -100;
}
boot.x = boot.x + 5; if (boot.x>1500)
{
boot.x = -100;
}
barrel.x = barrel.x + 4; if (barrel.x>1600)
{
barrel.x = -100;
}
}
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
rope.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
rope.y -= 10; hook.y -= 10;
}
if (downPressed)
{
rope.y += 10; hook.y += 10;
}
if (leftPressed)
{
rope.x -= 0; hook.x -= 0;
}
if (rightPressed)
{
rope.x += 0; hook.x += 0;
}
if (rope.y < 35)
{
rope.y = 35; hook.y = 100;
}
}
function fl_SetKeyPressed(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 fl_UnsetKeyPressed(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;
}
}
}

Actionscript 3 error 1009: Cannot access a property or method of a null object reference

I'm trying to make a simple game on Animate CC. Everything seems to work fine except when I look in the output, I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
at _2D_CW2_Game_v10_8_fla::MainTimeline/move()
at _2D_CW2_Game_v10_8_fla::MainTimeline/updateOb()
So I know where the issue might be, and I've been trying tweaking the code for days, googling possible solutions, but to no avail...
My entire source code is as below. Any feedback/suggestions will be greatly appreciated.
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundMixer;
//==================================================
// Variable declaration
//==================================================
// defines the variables for boundaries
var left:Number = 0;
var top:Number = 0;
var right:Number = stage.stageWidth;
var bottom:Number = stage.stageHeight;
var velX:Number = 0;
var velY:Number = 0;
var gravity:Number = 1;
var friction:Number = 0.8;
var bounce:Number = -0.5;
var score:Number = 2;
var cv:Number = 0;
var curCount:Number = 30; // countdown 30s
var rightKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var touchGround:Boolean = false;
// create and place player object on stage
var player:Player = new Player();
player.x = 110;
player.y = 460;
addChild(player);
// create obstacle array
var obstacles:Array = new Array();
var numOb:Number = 3;
// create and place enemies on stage
for (var i:Number = 0; i < numOb; i++) {
var ob:Npc = new Npc();
ob.x = 800;
ob.y = 470;
ob.scaleX = -1;
ob.vx = Math.random() * 20 + 1;
addChild(ob);
obstacles.push(ob);
}
//==================================================
// Event handlers
//==================================================
stage.addEventListener(Event.ENTER_FRAME, EntFrame);
addEventListener(Event.ENTER_FRAME, updateOb);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
//==================================================
// Functions
//==================================================
function keyDown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.RIGHT) {
rightKeyDown = true;
}
if (e.keyCode == Keyboard.LEFT) {
leftKeyDown = true;
}
if (e.keyCode == Keyboard.UP) {
// if player isn't already jumping and is on the ground
if (!upKeyDown && touchGround) {
// then start jumping
isJumping();
}
upKeyDown = true;
}
}
function keyUp(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.RIGHT) {
rightKeyDown = false;
}
if (e.keyCode == Keyboard.LEFT) {
leftKeyDown = false;
}
if (e.keyCode == Keyboard.UP) {
upKeyDown = false;
}
}
function EntFrame(e:Event):void {
player.x += velX;
player.y += velY;
velX *= friction;
velY += gravity;
if (player.y >= 450) {
touchGround = true;
player.y = 450;
}
// boundary checks
if (player.x + player.width/2 > right) {
player.x = right - player.width/2;
player.velX *= bounce;
} else if (player.x - player.width/2 < left) {
player.x = left + player.width/2;
player.velX *= bounce;
}
// make player move left or right
controls();
if (curCount > 0) {
cv++;
if (cv >= 30) {
curCount--;
cv = 0;
timertext.text = String(curCount);
if (curCount == 0) {
restart();
gotoAndStop("gameOverWon");
}
}
}
}
function updateOb(e:Event):void {
// make obstacles move
for (var i:Number = 0; i < numOb; i++) {
var ob:Npc = obstacles[i];
move(ob);
if (player.hitTestObject(obstacles[i])) {
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y - player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y - player.height/2, true))*/
bumpOb(obstacles[i]);
}
}
scoretext.text = String(score);
if (score == 0) {
restart();
gotoAndStop("gameOverLost");
}
}
// applies basic velocity to enemies
function move(moveOb) {
moveOb.x -= moveOb.vx;
if (moveOb.x + moveOb.width/2 > right) {
moveOb.x = right - moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = -1;
}
if (moveOb.x - moveOb.width/2 < left) {
moveOb.x = left + moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = 1;
}
}
function bumpOb(p) {
if (p) {
p.removeEventListener(Event.ENTER_FRAME, updateOb);
if (p.parent) {
removeChild(p);
score--;
}
}
}
function restart() {
if(contains(player)) {
removeChild(player);
}
for (var i:int = 0; i < numOb; i++) {
if (contains(obstacles[i]) && obstacles[i] != null) {
removeChild(obstacles[i]);
obstacles[i] = null;
}
}
// returns a new array that consists of a range of elements from the original array,
// without modifying the original array
obstacles.slice(0);
}
function controls() {
if (rightKeyDown) {
velX += 3;
player.scaleX = 1;
}
if (leftKeyDown) {
velX -= 3;
player.scaleX = -1;
}
}
function isJumping() {
touchGround = false;
velY = -15;
}
//==================================================
// Sound control for background music
//==================================================
btnMute.addEventListener(MouseEvent.CLICK, mute);
function mute(e:MouseEvent):void {
SoundMixer.soundTransform = new SoundTransform(0);
btnMute.removeEventListener(MouseEvent.CLICK, mute);
btnMute.addEventListener(MouseEvent.CLICK, unmute);
}
function unmute(e:MouseEvent):void {
SoundMixer.soundTransform = new SoundTransform(1);
btnMute.removeEventListener(MouseEvent.CLICK, unmute);
btnMute.addEventListener(MouseEvent.CLICK, mute);
}
I had the same problem when I created interactive elements for animation. Check which layer the interactive object is on. A similar error occurred when the object overlapped something that was located on the layer above.
You can try...
1) Your Npc is a class/library object, right?
Give the source MC/Sprite, the instance name of moveOb or p.
2) or else try... Use function parameters (this is a better coding style):
(2a) Since you say..
var ob:Npc = obstacles[i];
move(ob);
ps: why not simplify (without var) as : move( obstacles[i] ); ...?
(2b) Your move function should specify a data type together with your parameter name...
//# applies basic velocity to enemies
//# Wrong...
//function move(moveOb) {
//# Better...
function move( moveOb : Npc ) {
//# Aso fix as...
function bumpOb(p : Npc ) {
By using function parameters, you can now give unique names to the (function's) input parameters but stay referencing the same (or compatible) data type.
Let me know how it goes.
The obstacles array may have null elements in the middle. What if you add a condition to continue if it's null?
function updateOb(e:Event):void {
// make obstacles move
for (var i:Number = 0; i < obstacles.length; i++) {
var ob:Npc = obstacles[i];
if(!ob) continue;
move(ob);
if (player.hitTestObject(ob)) {
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y -
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y -
player.height/2, true))*/
bumpOb(obstacles[i]);
}
}
scoretext.text = String(score);
if (score == 0) {
restart();
gotoAndStop("gameOverLost");
}
}

How to move object in the direction of rotation in flash?

I have a "car" which has to be moved in the direction which it is rotated.
Now it just rotates in a direction and keeps on going up and down.Please help me.
I am using adobe flash pro cs6 and actionscript3.
My code is :
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
car.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
car.y += 5;
}
if (downPressed)
{
car.y -= 5;
}
if (rightPressed)
{
car.rotation += 5;
}
if (leftPressed)
{
car.rotation -= 5;
}
}
function fl_SetKeyPressed(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 fl_UnsetKeyPressed(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;
}
}
}
You need to use vector math to move the car's x,y based on an angle and a distance.
For example, you could move your car based on an angle and speed like this:
function move(degrees:Number, speed:Number):void {
var radians:Number = degrees * (Math.PI / 180);
car.x += Math.cos(radians) * speed;
car.y += Math.sin(radians) * speed;
}
Then you can use the car's rotation as the angle and 5 or -5 as the speed:
if (upPressed) {
move(car.rotation, 5);
}
if (downPressed) {
move(car.rotation, -5);
}
Note that this assumes rotation=0 means your car is facing right-ward. If you've draw your car facing a different direction you'll need to compensate for the angle you've drawn the car, for example if the car is facing upward you need to use move(car.rotation - 90, 5).

Flash cc as3 how to make a child object be effected the same way as the parent

I have generated a piece of code, with help of this message board, but it is one problem after another. This time I have it so when you click on an object a new object spawns, but I need it so the child (the one that has been spawned) is effected by the gravity physics in place.
Here is my code:
var drag:Number = 1;
var BlockAppear = new MovieClip;
gotoAndStop(1, "Minigame-Block");
HeartBear.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_4);
import flash.events.MouseEvent;
HBS.addEventListener(MouseEvent.CLICK, addHeart);
function addHeart(e:MouseEvent):void
{
var H=new HB;
H.y = 523;
H.x = 721;
addChild(H); //need the child to be effected by the gravity physics.
}
Block.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_3);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3);
function fl_MoveInDirectionOfKey_3(event:Event)
{
if (leftPressed)
{
Blockr.x -= 5;
}
if (rightPressed)
{
Block.x += 5;
}
}
function fl_SetKeyPressed_3(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed_3(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
function fl_ClickToDrag_4(event:MouseEvent):void
{
HeartBear.startDrag()
gravity = 0
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_4);
function fl_ReleaseToDrop_4(event:MouseEvent):void
{
HeartBear.stopDrag();
gravity = 0.9
}
var gravity = 0.8;
var floor = 523.3;
var onFloor:Boolean = false;
HeartBear.y = floor;
HeartBear.speedY = 0;
HeartBear.jump = 15;
stage.addEventListener(Event.ENTER_FRAME, enterframe);
function enterframe(e:Event) {
HeartBear.speedY += gravity;
HeartBear.y += HeartBear.speedY;
if(HeartBear.y > floor) {
HeartBear.speedY = 0;
HeartBear.y = floor;
onFloor = true; //gravity physics
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, space);
function space(e:KeyboardEvent) {
if(e.keyCode == Keyboard.UP && onFloor) {
HeartBear.speedY = -HeartBear.jump;
onFloor = false;
}
}
Shopclicker.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_3);
function fl_ClickToGoToScene_3(event:MouseEvent):void
{
MovieClip(this.root).gotoAndStop(1, "Shop");
}
Change
function enterframe(e:Event) {
HeartBear.speedY += gravity;
HeartBear.y += HeartBear.speedY;
if(HeartBear.y > floor) {
HeartBear.speedY = 0;
HeartBear.y = floor;
onFloor = true; //gravity physics
}
}
to
function enterframe(e:Event) {
e.currentTarget.speedY += gravity;
e.currentTarget.y += e.currentTarget.speedY;
if(e.currentTarget.y > floor) {
e.currentTarget.speedY = 0;
e.currentTarget.y = floor;
onFloor = true; //gravity physics
}
}
and change
stage.addEventListener(Event.ENTER_FRAME, enter frame);
to
HeartBear.addEventListener(Event.ENTER_FRAME, enterframe);
and change
function addHeart(e:MouseEvent):void
{
var H=new HB;
H.y = 523;
H.x = 721;
addChild(H); //need the child to be effected by the gravity physics.
}
to
function addHeart(e:MouseEvent):void
{
var H=new HB;
H.y = 523;
H.x = 721;
addChild(H); //need the child to be effected by the gravity physics.
H.addEventListener(Event.ENTER_FRAME, enterframe);
}
And try to camel-case your variable and function names as well as class names.

As3 How to flip a movieclip to face movement direction?

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
}