0

我想保留我的键盘事件,但也可以添加鼠标事件。我的游戏键盘事件工作正常我只是不知道如何向它们添加鼠标事件。

我的舞台上有一些电影剪辑,左右上下等。

Character.as

[code]
if( m_keyboard.IsKeyDown( eKeyCodes.kLeftArrow ) )
{
    walking = true;
    x += 1;
    this.scaleX = 1;    
}

if( m_keyboard.IsKeyDown( eKeyCodes.kRightArrow ) )
etc....
[code]

eKeyCodes.as

[code]
static public const kLeftArrow:int = 37;
static public const kRightArrow:int = 39;

etc....
[/code]

Keyboard.as
[/code]
public class Keyboard
{
    private const kNumKeyCodes:int = 255;

    private var m_liveKeyState:Vector.<Boolean>;
    private var m_keyState:Vector.<Boolean>;
    private var m_lastKeyState:Vector.<Boolean>;

    public function Keyboard( stage:Stage )
    {
        // add handlers
        stage.addEventListener( KeyboardEvent.KEY_DOWN, OnKeyDown, false, 0, true );
        stage.addEventListener( KeyboardEvent.KEY_UP, OnKeyUp, false, 0, true );

        // vector is an array of booleans , the array is length is passed into the constructor as 255.
        m_keyState = new Vector.<Boolean>( kNumKeyCodes );
        m_lastKeyState = new Vector.<Boolean>( kNumKeyCodes );
        m_liveKeyState = new Vector.<Boolean>( kNumKeyCodes );

        // clear state
        for ( var i:int = 0; i<kNumKeyCodes; i++ )
        {
            // the whole array is false;
            m_keyState[i] = false;
            m_lastKeyState[i] = false;
            m_liveKeyState[i] = false;
        }



    }

    public function Update( ):void
    {

        for ( var i:int = 0; i<kNumKeyCodes; i++ )
        {
            m_lastKeyState[i] = m_keyState[i];
            m_keyState[i] = m_liveKeyState[i];
        }
    }

    private function OnKeyDown( e:KeyboardEvent ):void
    {

        m_liveKeyState[e.keyCode] = true;
        trace("m_liveKeyState = " + e.keyCode);
    }

    private function OnKeyUp( e:KeyboardEvent ):void
    {

        m_liveKeyState[e.keyCode] = false;
    }

    public function IsKeyDown( key:int ):Boolean           
    {
        // if key is down leftarrow ( 36 ) && 36 < 255 do no error; as key is a valid key;
        Assert( key >= 0 && key <= kNumKeyCodes, "Keyboard.IsKeyDown(): invalid key!" );

        return m_keyState[key];

    }

    /// Is key transitioning 

    public function IsKeyDownTransition( key:int ):Boolean
    {
        Assert( key >= 0 && key <= kNumKeyCodes, "Keyboard.IsKeyDownTransition(): invalid key!" );

        return !m_lastKeyState[key] && m_keyState[key]; 
    }
}
}
[/code]

我在想类似的东西......

TouchButtons.as
[code]
 icon.leftButton.addEventListner(MouseEvent.MOUSE_DOWN, leftButtonPressed, false, 0, true);
 icon.RightButton.addEventListner(MouseEvent.MOUSE_DOWN, rightButtonPressed, false, 0, true);


 public function leftButtonPressed( event:MouseEvent ):void
 {
      m_keyboard.IsKeyDown( eKeyCodes.kLeftArrow  );
 }
 etc...
[code]

但这似乎不起作用,有没有一种简单的方法可以做到这一点。

4

2 回答 2

0

我猜你希望它像舞台活动一样工作。

public function leftButtonPressed( event:MouseEvent ):void
{
      m_liveKeyState[eKeyCodes.kLeftArrow] = true;
 }
于 2013-06-15T10:24:22.533 回答
0

朋友快疯了,我想将此代码转换为在触摸屏中使用

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 leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(70, -50);
var upBumpPoint:Point = new Point(0, -110);
var downBumpPoint:Point = new Point(0, 0);

var scrollX:Number = 0;
var scrollY:Number = 500;

var xSpeed:Number = 0;
var ySpeed:Number = 0;

var speedConstant:Number = 2;
var frictionConstant:Number = 0.9;
var gravityConstant:Number = 1.8;
var jumpConstant:Number = -30;
var maxSpeedConstant:Number = 18;

var doubleJumpReady:Boolean = false;
var upReleasedInAir:Boolean = false;

var keyCollected:Boolean = false;
var doorOpen:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

stage.addEventListener(Event.ENTER_FRAME, loop);

function loop(e:Event):void{
	if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
		//trace("leftBumping");
		leftBumping = true;
	} else {
		leftBumping = false;
	}
	
	if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
		//trace("rightBumping");
		rightBumping = true;
	} else {
		rightBumping = false;
	}
	
	if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
		//trace("upBumping");
		upBumping = true;
	} else {
		upBumping = false;
	}
	
	if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
		//trace("downBumping");
		downBumping = true;
	} else {
		downBumping = false;
	}	
	

	if(leftPressed){
		xSpeed -= speedConstant;
		player.scaleX = -1;

	} else if(rightPressed){
		xSpeed += speedConstant;
		player.scaleX = 1;
	}


	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 we are touching the floor
		if(ySpeed > 0){ 
			ySpeed = 0; //set the y speed to zero
		}
		if(upPressed){ //and if the up arrow is pressed
			ySpeed = jumpConstant; //set the y speed to the jump constant
		}
		
		//DOUBLE JUMP
		if(upReleasedInAir == true){
			upReleasedInAir = false;
		}
		if(doubleJumpReady == false){
			doubleJumpReady = true;
		}
	} else { //if we are not touching the floor
		
		ySpeed += gravityConstant; //accelerate downwards
		
		//DOUBLE JUMP
		if(upPressed == false && upReleasedInAir == false){
			upReleasedInAir = true;
			//trace("upReleasedInAir");
		}
		if(doubleJumpReady && upReleasedInAir){
			if(upPressed){ //and if the up arrow is pressed
				//trace("doubleJump!");
				doubleJumpReady = false;
				ySpeed = jumpConstant; //set the y speed to the jump constant
			}
		}
		
	}
	
	if(keyCollected == false){
		if(player.hitTestObject(back.doorKey)){
			back.doorKey.visible = false;
			keyCollected = true;
			trace("key collected");
		}
	}
	
	if(doorOpen == false){
		if(keyCollected == true){
			if(player.hitTestObject(back.lockedDoor)){
				back.lockedDoor.gotoAndStop(2);
				doorOpen = true;
				trace("door open");
			}
		}
	}
											 
	
	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;
	
	sky.x = scrollX * 0.2;
	sky.y = scrollY * 0.2;
	
}

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

于 2015-05-23T23:32:06.290 回答