Actionscript 3 在AS3中,我将如何添加2个按钮,使玩家能够按而不是使用箭头键移动?

Actionscript 3 在AS3中,我将如何添加2个按钮,使玩家能够按而不是使用箭头键移动?,actionscript-3,Actionscript 3,我从互联网上获得了这个代码,用于游戏Pong为我的作业编写AS3文档。不过,我基本上是一个代码初学者,我正在尝试让这个游戏在手机上运行,因为作业需要一个游戏来运行它。 因为它使用箭头键,所以我想基本上用按钮代替它们,一个用于向上,一个用于向下。我只是不知道哪种代码可以让我这么做。 比如,当按下按钮时,播放器会根据哪个按钮向上或向下移动,但我不确定该在哪里替换代码以及要删除什么 以下是“Pong”类文件: package { import flash.display.MovieClip;

我从互联网上获得了这个代码,用于游戏Pong为我的作业编写AS3文档。不过,我基本上是一个代码初学者,我正在尝试让这个游戏在手机上运行,因为作业需要一个游戏来运行它。 因为它使用箭头键,所以我想基本上用按钮代替它们,一个用于向上,一个用于向下。我只是不知道哪种代码可以让我这么做。 比如,当按下按钮时,播放器会根据哪个按钮向上或向下移动,但我不确定该在哪里替换代码以及要删除什么

以下是“Pong”类文件:

package {

    import flash.display.MovieClip;

    import flash.events.KeyboardEvent;

    import flash.ui.Keyboard;

    import flash.events.Event;

    public class Pong extends MovieClip {
        //constants

        private var pongUp:MovieClip = new PongUp  ;

        private var pongDown:MovieClip = new PongDown  ;

        const ballspeed:int = 10;
        const playerspeed:int = 7;
        const computerspeed:int = 10;
        const computerIntelligence:int = 7;//intelligence is 7 out of 10

        //global variables
        var vx:int =  -  ballspeed;// x component of velocity of ball (velocity is speed with direction)
        var vy:int = ballspeed;// y component of velocity of ball
        var v1:int = 0;// initial velocity of player
        var v2:int = 0;// initial velocity of computer
        var playerScore:int = 0;
        var computerScore:int = 0;

        var player:MovieClip = new PongPlayer  ;
        var computer:MovieClip = new PongComputer  ;
        var ball:MovieClip = new PongBall  ;

        public function Pong() {
            //init();       
            addEventListener(Event.ADDED_TO_STAGE,init);

        }
        //this function will add all event listeners
        function init(e:Event):void {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
            stage.addEventListener(Event.ENTER_FRAME,EnterFrame);

            addChild(player);
            addChild(computer);
            addChild(ball);

            player.x = 23;
            player.y = 300;

            computer.x = 637;
            computer.y = 311;

            ball.x = 308;
            ball.y = 328;

            addChild(pongUp);

            pongUp.x = 25;
            pongUp.y = 700;

            addChild(pongDown);

            pongDown.x = 530;
            pongDown.y = 700;


        }
        // this function resets the game
        function reset():void {
            player.y = stage.stageHeight / 2;
            computer.y = stage.stageHeight / 2;
            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;
            if (Math.abs(Math.random() * 2) > 1)
            {
                vx =  -  ballspeed;
            }
            else
            {
                vx = ballspeed;
            }
            if (Math.abs(Math.random() * 2) > 1)
            {
                vy =  -  ballspeed;
            }
            else
            {
                vy = ballspeed;
            }
        }


        //pongDown.addEventListener ( MouseEvent.MOUSE_DOWN,moveDown );

        //function moveDown ( e:MouseEvent ): void

        //{





        //}     
        //this function sets the velocity of player when key is pressed  
        function KeyDown(event:KeyboardEvent):void {
            if (event.keyCode == Keyboard.UP)
            {
                v1 =  -  playerspeed;
            }
            else if (event.keyCode == Keyboard.DOWN)
            {
                v1 = playerspeed;
            }
        }
        //this function sets the velocity of player to 0 if key is released 
        function KeyUp(event:KeyboardEvent):void {
            if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
            {
                v1 = 0;
            }
        }

        //This function is executed when a frame changes
        function EnterFrame(event:Event):void {
            //variable decleration
            var pHalfHeight = player.height / 2;// half height of player(used for collisions)
            var pHalfWidth = player.width / 2;// half width of player (used for collisions)
            var bHalfHeight = ball.height / 2;// half height of ball(used for collisions)
            var bHalfWidth = ball.width / 2;// half width of ball (used for collisions)

            //moving the player
            player.y +=  v1;
            //limiting the motion of player (it should not move beyond the stageheight)
            if (player.y + pHalfHeight > stage.stageHeight)
            {
                player.y = stage.stageHeight - pHalfHeight;
            }
            else if (player.y - pHalfHeight < 0)
            {
                player.y = 0 + pHalfHeight;
            }

            //moving the ball
            ball.x +=  vx;
            ball.y +=  vy;

            //moving the computer automatically
            if (Math.abs(Math.random() * 10) < computerIntelligence)
            {
                var d:int = computer.y - ball.y;
                if (Math.abs(d) > pHalfHeight)
                {
                    if ((d > 0))
                    {
                        v2 =  -  computerspeed;
                    }
                    else
                    {
                        v2 = computerspeed;
                    }
                }
            }
            computer.y +=  v2;
            //limiting the motion of computer (it should not move beyond the stageheight)
            if (computer.y + pHalfHeight > stage.stageHeight)
            {
                computer.y = stage.stageHeight - pHalfHeight;
            }
            else if (computer.y - pHalfHeight < 0)
            {
                computer.y = 0 + pHalfHeight;
            }

            //collision with horizontal walls
            if (ball.y + bHalfHeight >= stage.stageHeight || ball.y - bHalfHeight <= 0)
            {
                vy *=  -1;
            }

            //collision with player and computer
            if (ball.x - bHalfWidth <= player.x + pHalfWidth)
            {
                if (Math.abs(ball.y - player.y) <= pHalfHeight)
                {
                    vx = ballspeed;
                    if ((v1 != 0))
                    {
                        vy = 2 * v1;
                    }
                }
            }
            else if (ball.x + bHalfWidth >= computer.x - pHalfWidth)
            {
                if (Math.abs(ball.y - computer.y) <= pHalfHeight)
                {
                    vx =  -  ballspeed;
                    if ((v2 != 0))
                    {
                        vy = v2;
                    }
                }
            }

            //collision with vertical walls & updating scores
            if (ball.x + bHalfWidth >= stage.stageWidth)
            {
                playerScore +=  1;
                reset();
            }
            else if (ball.x - bHalfWidth <= 0)
            {
                computerScore +=  1;
                reset();
            }

            //display the score on the textfield
            //txtPlayer.text  = String(playerScore);
            //txtComputer.text = String(computerScore);
        }
    }

}
包{
导入flash.display.MovieClip;
导入flash.events.KeyboardEvent;
导入flash.ui.Keyboard;
导入flash.events.Event;
公开课乒乓球扩展了MovieClip{
//常数
private var pongUp:MovieClip=新pongUp;
private var pongDown:MovieClip=新pongDown;
恒球速:int=10;
常量播放速度:int=7;
常数计算机速度:int=10;
const computerIntelligence:int=7;//智能是十分之七
//全局变量
var vx:int=-ballspeed;//x球的速度分量(速度是有方向的速度)
var vy:int=球速;//球速的y分量
var v1:int=0;//玩家的初始速度
var v2:int=0;//计算机的初始速度
var playerScore:int=0;
var计算机分数:int=0;
var播放器:MovieClip=新PongPlayer;
var计算机:MOVICLIP=新的PongComputer;
var ball:MovieClip=新的PongBall;
公共功能Pong(){
//init();
addEventListener(事件。添加到\u阶段,初始化);
}
//此函数将添加所有事件侦听器
函数init(e:事件):void{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY\u UP,KeyUp);
stage.addEventListener(事件。输入帧,输入帧);
addChild(玩家);
addChild(计算机);
addChild(ball);
player.x=23;
player.y=300;
计算机x=637;
计算机y=311;
球x=308;
球y=328;
addChild(pongUp);
pongUp.x=25;
pongUp.y=700;
addChild(邦镇);
pongDown.x=530;
pongDown.y=700;
}
//此函数用于重置游戏
函数重置():void{
player.y=stage.stageHeight/2;
computer.y=stage.stageHeight/2;
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
if(Math.abs(Math.random()*2)>1)
{
vx=球速;
}
其他的
{
vx=球速;
}
if(Math.abs(Math.random()*2)>1)
{
vy=球速;
}
其他的
{
vy=球速;
}
}
//pongDown.addEventListener(MouseEvent.MOUSE_DOWN,moveDown);
//函数下移(e:MouseeEvent):无效
//{
//}     
//此功能设置按键时播放器的速度
功能键关闭(事件:KeyboardEvent):无效{
if(event.keyCode==Keyboard.UP)
{
v1=球员速度;
}
else if(event.keyCode==Keyboard.DOWN)
{
v1=玩家速度;
}
}
//如果释放按键,此功能将播放器的速度设置为0
功能键控(事件:KeyboardEvent):无效{
if(event.keyCode==Keyboard.UP | | event.keyCode==Keyboard.DOWN)
{
v1=0;
}
}
//此功能在帧更改时执行
函数EnterFrame(事件:事件):无效{
//可变减容
var pHalfHeight=player.height/2;//玩家的半高(用于碰撞)
var pHalfWidth=player.width/2;//播放器的一半宽度(用于碰撞)
var bHalfHeight=ball.height/2;//球的半高(用于碰撞)
var bHalfWidth=ball.width/2;//球的一半宽度(用于碰撞)
//移动玩家
player.y+=v1;
//限制玩家的运动(不应超出舞台高度)
if(player.y+pHalfHeight>stage.stageHeight)
{
player.y=stage.stageHeight-pHalfHeight;
}
else if(player.y-PhalfHight<0)
{
player.y=0+pHalfHeight;
}
//移动球
ball.x+=vx;
ball.y+=vy;
//自动移动计算机
if(Math.abs(Math.random()*10)高度)
{
如果((d>0))
{
v2=计算机速度;
}
其他的
{
v2=计算机速度;
}
}
}
计算机y+=v2;
//限制计算机的运动(不应超出舞台高度)
if(计算机y+pHalfHeight>stage.stageHeight)
{
computer.y=stage.stageHeight-pHalfHeight;
}
else if(计算机y-PhalfHight<0)
{
计算机y=0+八分之一;
}
//c
//this function sets the velocity of player when key is pressed  
    function KeyDown(event:KeyboardEvent):void {
        if (event.keyCode == Keyboard.UP)
        {
            v1 =  -  playerspeed;
        }
        else if (event.keyCode == Keyboard.DOWN)
        {
            v1 = playerspeed;
        }
    }
    //this function sets the velocity of player to 0 if key is released 
    function KeyUp(event:KeyboardEvent):void {
        if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
        {
            v1 = 0;
        }
    }
function KeyDown(event:TouchEvent):void {
    if (event.stageY < player.y){
        v1 =  -  playerspeed;
    }else if (event.stageY > player.y){
        v1 = playerspeed;
    }
}

function KeyUp(event:TouchEvent):void {
    v1 = 0;
}
//this function will add all event listeners
    function init(e:Event):void {
        stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
//this function will add all event listeners
    function init(e:Event):void {
        stage.addEventListener(TouchEvent.TOUCH_BEGIN, KeyDown);
        stage.addEventListener(TouchEvent.TOUCH_END, KeyUp);
import flash.events.KeyboardEvent;
import flash.events.TouchEvent;