samedi 21 janvier 2017

In AS3 how would i add 2 buttons that would enable the player to move on press instead of using the arrow keys?

I got this code off the internet for the game Pong to work on my AS3 document for my assignment. However i'm pretty much a beginner at code and i'm trying to get this game to work on mobile as the assignment needs a game to work on it. Because it uses arrow keys, i would like to basically just replace them with buttons instead, one for going up and one for going down. I just don't know the type of code that would allow me to do that. Something like, when button is pressed, player moves up or down depending which button, but im not sure where to replace the code and what to get rid of.

Here's the class file:

package {

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event;

public class Pong extends MovieClip {
    //constants
    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);  // mouse events here
    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

    }
    // 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; 
        }
    }
    //this function sets the velocity of player when key is pressed  // start drag with mouse down
    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 // Stopdrag here
    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);
    }
}

}

Aucun commentaire:

Enregistrer un commentaire