"player" is an object that moves up , down , left and right when I press on arrow keys.
"player" doesn't move diagonally when I press up and left keys together or any other couple of keys.
I'm using Adobe flash CS5 and action script 3 ( As3 ), Would you tell me what the solution is ?
stage.addEventListener(KeyboardEvent.KEY_DOWN, detectText);
function detectText(myevent:KeyboardEvent):void {
var up:Boolean = false;
var down:Boolean = false;
var left:Boolean = false;
var right:Boolean = false;
var speedOfplayer:Number = 5 ;
if (myevent.keyCode==39){
right = true ;
}
if (myevent.keyCode==37){
left = true ;
}
if (myevent.keyCode==38){
up = true ;
}
if (myevent.keyCode==40){
down = true ;
}
// if(right is true and left is not true)
if( right && !left ) {
player.x = player.x + speedOfplayer;
}
// if(up is true and down is not true)
if( up && !down ) {
player.y = player.y - speedOfplayer;
}
// if(down is true and up is not true)
if( down && !up ) {
player.y = player.y + speedOfplayer;
}
// if(down is true and up is not true)
if( left && !right ) {
player.x = player.x - speedOfplayer;
}
// Move diagonally
if( left && up && !right && !down ) {
player.y = player.y - speedOfplayer;
player.x = player.x - speedOfplayer;
}
if( right && up && !left && !down ) {
player.x = player.x + speedOfplayer;
player.y = player.y - speedOfplayer;
}
if( left && down && !right && !up ) {
player.x = player.x - speedOfplayer;
player.y = player.y - speedOfplayer;
}
if( right && down && !left && !up ) {
player.x = player.x + speedOfplayer;
player.y = player.y + speedOfplayer;
}
You may be overcomplicating things. This can be done in the following way. What is happening to the KEY_DOWN listener is that it will report the last key pressed. So you need a ENTER_FRAME listener to move the object. And because ENTER_FRAME will keep calling the function provided you need to capture if a key is released to reset the speed of the player to 0.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.addEventListener(Event.ENTER_FRAME, onTick);
var speedX:uint = 0;
var speedY:uint = 0;
function onKeyDown(event:KeyboardEvent):void {
var key:uint= event.keyCode;
if (key == Keyboard.UP) {
speedY = -5;
}
if (key == Keyboard.DOWN) {
speedY = 5;
}
if (key == Keyboard.LEFT) {
speedX = -5;
}
if (key == Keyboard.RIGHT) {
speedX = 5;
}
}
function onKeyUp(event:KeyboardEvent):void {
var key:uint= event.keyCode;
if (key == Keyboard.UP || key == Keyboard.DOWN) {
speedY = 0;
}
if (key == Keyboard.LEFT || key == Keyboard.RIGHT) {
speedX = 0;
}
}
function onTick(event:Event):void {
player.x += speedX;
player.y += speedY;
}
Related
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");
}
}
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.
Im trying to get a two player version of Snake running but i am having trouble getting the second snake to work, player 1 plays with w,a,s and d while player 2 uses the arrow keys. Player 1 with w,a,s and d is working player 2 with the arrows does not. Instead the arrow keys control player 1 as well as w,a,s and d.I think it has to do with the onEnterFrame function but i've been starring at it for days and i cannot see anything. Im fairly new to ActionScript as well so some hints to point me in the right direction is all i need, Thanks!
package{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event
public class Main extends MovieClip{
const speed:int = 5;//speed of the snake
var vx:int;
var vy:int;
var gFood:Food;
var gFood2:Food2;
var head:SnakePart;
var head2:SnakePart2;
var SnakeDirection:String;
var Snake2Direction:String;
var snake:Array;
var snake2:Array;
public function Main(){
init();
}
function init():void {
//Initialize everything!
vx = 1; vy = 0;
snake = new Array();
snake2 = new Array();
SnakeDirection = "";
Snake2Direction = "";
//add food to the stage
addFood();
addFood2();
//add snakes head to the stage
head = new SnakePart();
head.x = stage.stageWidth/2;
head.y = stage.stageHeight/2;
snake.push(head);
addChild(head);
head2 = new SnakePart2();
head2.x = stage.stageWidth/14;
head2.y = stage.stageHeight/25;
snake2.push(head2);
addChild(head2);
stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown);
addEventListener(Event.ENTER_FRAME , onEnterFrame);
//ENTER_FRAME listener is attached to main class and not to the stage directly
}
//This function will add food to the stage
function addFood():void {
gFood = new Food();
gFood.x = 50 + Math.random()*(stage.stageWidth-100);
gFood.y = 50 + Math.random()*(stage.stageHeight-100);
addChild(gFood);
}
function addFood2():void {
gFood2 = new Food2();
gFood2.x = 50 + Math.random()*(stage.stageWidth-100);
gFood2.y = 50 + Math.random()*(stage.stageHeight-100);
addChild(gFood2);
}
//this function will reset the game
function reset():void {
removeChild(gFood);
addFood();
head.x = stage.stageWidth/2;
head.y = stage.stageHeight/2;
vx = 1;vy = 0;
removeChild(gFood2);
addFood2();
head.x = stage.stageWidth/4;
head.y = stage.stageHeight/4;
vx = 1;vy = 0;
for(var i = snake.length-1;i>0;--i){
removeChild(snake[i]);
snake.splice(i,1);
}
}
function onKeyDown(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A) {
SnakeDirection = "a";
} else if (event.keyCode == Keyboard.D) {
SnakeDirection = "d";
} else if (event.keyCode == Keyboard.W) {
SnakeDirection = "w";
} else if (event.keyCode == Keyboard.S) {
SnakeDirection = "s";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT) {
Snake2Direction = "left";
} else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
} else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
} else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D || event.keyCode == Keyboard.W || event.keyCode == Keyboard.S) {
SnakeDirection = "";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT ||event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {
Snake2Direction = "";
}
}
function onEnterFrame(event:Event):void {
//setting direction of velocity
if(SnakeDirection == "a" && vx != 1) {
vx = -1;
vy = 0;
}else if(SnakeDirection == "d" && vx != -1) {
vx = 1;
vy = 0;
}else if(SnakeDirection == "w" && vy != 1) {
vx = 0;
vy = -1;
}else if(SnakeDirection == "s" && vy != -1) {
vx = 0;
vy = 1;
}
//setting direction of velocity
if(Snake2Direction == "left" && vx != 1) {
vx = -1;
vy = 0;
}else if(Snake2Direction == "right" && vx != -1) {
vx = 1;
vy = 0;
}else if(Snake2Direction == "up" && vy != 1) {
vx = 0;
vy = -1;
}else if(Snake2Direction == "down" && vy != -1) {
vx = 0;
vy = 1;
}
//collison with stage
if(head.x - head.width/2 <= 0){
reset();
}
if(head.x + head.width/2 >= stage.stageWidth){
reset();
}
if(head.y - head.height/2 <= 0){
reset();
}
if(head.y + head.height/2 >= stage.stageHeight){
reset();
}
//move body of the snake
for(var i = snake.length-1;i>0;--i){
snake[i].x = snake[i-1].x;
snake[i].y = snake[i-1].y;
}
for(var i = snake2.length-1;i>0;--i){
snake2[i].x = snake2[i-1].x;
snake2[i].y = snake2[i-1].y;
}
//changing the position of snake's head
head.x += vx*speed;
head.y += vy*speed;
//collision with tail
for(var i = snake.length-1;i>=1;--i){
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
reset();
break;
}
}
for(var i = snake2.length-1;i>=1;--i){
if(snake2[0].x == snake2[i].x && snake2[0].y == snake2[i].y){
reset();
break;
}
}
//collision with food player 1
if(head.hitTestObject(gFood)){
removeChild(gFood);
addFood();
var bodyPart = new SnakePart();
bodyPart.x = snake[snake.length - 1].x;
bodyPart.y = snake[snake.length - 1].y;
snake.push(bodyPart);
addChild(bodyPart);
}
if(head.hitTestObject(gFood2)){
removeChild(gFood2);
addFood2();
var bodyPart = new SnakePart();
bodyPart.x = snake[snake.length - 1].x;
bodyPart.y = snake[snake.length - 1].y;
snake.push(bodyPart);
addChild(bodyPart);
}
//collision with food player 2
if(head.hitTestObject(gFood)){
removeChild(gFood);
addFood();
var bodyPart = new SnakePart2();
bodyPart.x = snake2[snake2.length - 1].x;
bodyPart.y = snake2[snake2.length - 1].y;
snake2.push(bodyPart);
addChild(bodyPart);
}
if(head.hitTestObject(gFood2)){
removeChild(gFood2);
addFood2();
var bodyPart = new SnakePart2();
bodyPart.x = snake2[snake2.length - 1].x;
bodyPart.y = snake2[snake2.length - 1].y;
snake2.push(bodyPart);
addChild(bodyPart);
}
}
}
}
First,let me explain your mistakes in this code:
1.You have the same vx and vy for both snakes !! this means both snakes should go parallel and to the same direction !!
2.and problem 1 won't happen because you set head.x and y but not head2 for the other snake!
Just for being careless... :)
so:
1.add variables vx2 and vy2 for the poor second snake !
2.replace the second setting direction of velocity with this:
if(Snake2Direction == "left" && vx2 != 1) {
vx2 = -1;
vy2 = 0;
}else if(Snake2Direction == "right" && vx2 != -1) {
vx2 = 1;
vy2 = 0;
}else if(Snake2Direction == "up" && vy2 != 1) {
vx2 = 0;
vy2 = -1;
}else if(Snake2Direction == "down" && vy2 != -1) {
vx2 = 0;
vy2 = 1;
}
3.do anything you left for the second snake.(head2.x and y,collision for second snake, etc)
I H☺P E this helps !
My player stops moving when two keys are pressed at the same time. But the animation still moves. For example, if I press up and down at the same time or right and left at the same time.
On key down event listener:
if (event.keyCode == Keyboard.D)
{
isRight = true
}
if (event.keyCode == Keyboard.A)
{
isLeft = true
}
if (event.keyCode == Keyboard.W)
{
isUp = true
}
if (event.keyCode == Keyboard.S)
{
isDown = true
}
On key up event listener:
if (event.keyCode == Keyboard.D)
{
isRight = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.A)
{
isLeft = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.W)
{
isUp = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.S)
{
isDown = false
gotoAndStop(1);
}
On enterframe:
if (isRight == true)
{
x += 5;
play();
}
if (isLeft == true )
{
x -= 5;
play();
}
if (isUp == true)
{
y -= 5;
play();
}
if (isDown == true)
{
y += 5;
play();
}
If players goes x -= 1 and x += 1 it basically moves x += 0 overall. We can easily check that and stop animation in needed:
var iP:Point = new Point(x,y);//try to avoid creating new objects on frame interval
if (isRight) x += 5;
if (isLeft) x -= 5;
if (isUp) y -= 5;
if (isDown) y += 5;
if(!Point.distance(iP,new Point(x,y)) goToAndStop(1);
else play();
I don't see any checks to see if more than 1 key is being pressed?
surely you should introduce something like a keycount into the enterframe:
var count:uint = 0;
if (isRight == true){
count++
x += 5;
}
if (isLeft == true ){
count++;
x -= 5;
}
if (isUp == true){
count++;
y -= 5;
}
if (isDown == true){
count++
y += 5;
}
if (count > 1) {
isRight = isLeft = isUp = isDown = false;
gotoAndStop(1);
} else {
play();
}
My object "araba" is not moving when KEY_DOWN happening waiting for your comments thx !
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.Bitmap;
stage.addEventListener(KeyboardEvent.KEY_DOWN, basili);
stage.addEventListener(KeyboardEvent.KEY_UP, degil);
stage.addEventListener(Event.ENTER_FRAME, giris);
var yukari:Boolean = false;
var asagi:Boolean = false;
var sola:Boolean = false;
var saga:Boolean = false;
var hiz:Number = 5;
var araba:MovieClip=new araba();
var masa:MovieClip =new masa();
function giris(event:Event):void
{
if ( sola && !saga ) {
araba.x -= hiz;
araba.rotation = 270;
}
if( saga && !sola ) {
araba.x +=hiz;
araba.rotation = 90;
}
if( yukari && !asagi ) {
araba.y -= hiz;
araba.rotation = 0;
}
if( asagi && !yukari ) {
araba.y += hiz;
araba.rotation = 180;
}
if( sola && yukari && !saga && !asagi ) {
araba.rotation = 315;
}
if( saga && yukari && !sola && !asagi ) {
araba.rotation = 45;
}
if( sola && asagi && !saga && !yukari ) {
araba.rotation = 225;
}
if( saga && asagi && !sola && !yukari) {
araba.rotation = 135;
}
if( araba.y < masa.y ){
araba.y = masa.height;
}
if( araba.y > masa.height ){
araba.y = masa.y;
}
if( araba.x < masa.x ){
araba.x = masa.width;
}
if( araba.x > masa.width ){
araba.x = masa.x;
}
}
function basili (event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
yukari = true;
break;
case Keyboard.DOWN:
asagi = true;
break;
case Keyboard.LEFT:
sola = true;
break;
case Keyboard.RIGHT:
saga = true;
break;
}
}
function degil(event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
yukari = false;
break;
case Keyboard.DOWN:
asagi = false;
break;
case Keyboard.LEFT:
sola = false;
break;
case Keyboard.RIGHT:
saga = false;
break;
}
}
<code>
I've looked at your code and it looks fine to me, but why don't you try tracing and see what happens?
private var leftKeyIsDown:Boolean;
private var rightKeyIsdown:Boolean
private var downKeyIsDown:Boolean;
private var upKeyIsDown:Boolean;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); //when key is down, go to this function
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); //when key is up, go to this function
gameloop();
private function gameLoop(e:Event):void //this function runs 30 times every second, due to 30fps
{
playerControl();
}
private function keyDown(e:KeyboardEvent):void
{
//trace Event and KeyCode, when we trace this we get I.D number from key
//trace(e.keyCode);
if (e.keyCode == 37)
{
//left key is pressed
leftKeyIsDown = true;
}
if (e.keyCode == 38)
{
//up key is pressed
upKeyIsDown = true;
}
if (e.keyCode == 39)
{
//right key is pressed
rightKeyIsdown = true;
}
if (e.keyCode == 40)
{
//down key is pressed
downKeyIsDown = true;
}
}
private function keyUp(e:KeyboardEvent):void
{
//if our left key is released
if (e.keyCode == 37)
{
//left key is released
leftKeyIsDown = false;
}
if (e.keyCode == 38)
{
//up key is released
upKeyIsDown = false;
}
if (e.keyCode == 39)
{
//right key is released
rightKeyIsdown = false;
}
if (e.keyCode == 40)
{
//down key is released
downKeyIsDown = false;
}
}
private function playerControl():void
{
//if left is currently down
if (leftKeyIsDown)
{
//move ship to the left
ship.x -= 5; //subtract 5 from ships position evey loop,
}
//if right is currently down
if (rightKeyIsdown)
{
//move ship to the right
ship.x += 5; //subtract 5 from ships position evey loop,
}
//if up is currently down
if (upKeyIsDown)
{
//move ship up
ship.y -= 5; //subtract 5 from ships position evey loop,
}
//if left is currently down
if (downKeyIsDown)
{
//move ship down
ship.y += 5; //subtract 5 from ships position evey loop,
}
}
replace ship with your object.
This is a template, i've commented too.
Please tell me if this is useful, it doesn't fix your code, I know.
But hopefully you can adapt this to it.