Using the existing files on last workshop: pong.fla & main.as
Change: width*height: 1024*768 frame rate(fps) : 25
Create: player class: 'player.as'
define the player's width and height
public var playerWidth: Number = 30;
public var playerHeight: Number = 100;
provide the value for xPosition and yPosition and set player on stage
//position the player on the stage
this.x = xPosition;
this.y = yPosition;
draw player (player.as part)
this.drawPlayer();
...
public function drawPlayer() {
// draw the player on the stage by filling a rectangle
this.graphics.beginFill(0x452506, 1);
this.graphics.drawRect
(
0-this.playerWidth/2,
0-this.playerHeight/2,
this.playerWidth,
this.playerHeight
);
}
add player to the stage (main.as part)
var player1;
var player2;
...
player1 = new player(num, num);
player2 = new player(num, num);
stage.addChild(player1);
stage.addChild(player2);
in the player class, add two functions to get the player's current position and get the player's moved position
public function setY(newY)
{
//set a new Y position for the player
this.y = newY;
}
public function getY()
{
//return the current y value for the player
return this.y;
}
set the keypress event in main.as
import flash.events.*;
...
stage.addEventListener(Keyboard.KEY_DOWN, movePlayer);
set the key pressed movement of player1 and player2
public function movePlayer(e: KeyboardEvent) {
//get the key pressed and then move the player
var speed = 8;
switch (e.keyCode) {
case 87:
// W pressed and player 1 moves up
player1.setY(player1.getY() - speed);
break;
case 83:
// S pressed and player 1 moves down
player1.setY(player1.getY() + speed);
break;
case 38:
// Up pressed and player 2 moves up
player2.setY(player2.getY() - speed);
break;
case 40:
// Down pressed and player 2 moves down
player2.setY(player2.getY() + speed);
break;
default:
trace('Invalid key pressed')
break;
}
The rest of the code might not be recorded simultaneously but would be done at spare time.