The software I'm using is FlashDevelop 4.0.1.
Here's the code:
Main:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author Migs Rebueno
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
trace("Hello World!\n");
trace("Welcome to FlashDevelop!");
var myString: String = "Score: ";
trace(myString + 10);
trace("Watch the blue ball bounce!!");
var GGClass: GameGlobals = new GameGlobals();
addChild(GGClass);
var GameClass: Game = new Game();
addChild(GameClass);
}
}
}
Class codes:
Circle (which is the ball):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.text.engine.JustificationStyle;
import flash.utils.getTimer;
/**
* ...
* @author Migs Rebueno
*/
public class Circle extends Sprite
{
public var location: Point = new Point();
public var radius: Number = 50;
public var vector:Point;
public var speed: Number = 400;
public var prevTime: Number;
public var hit: Boolean = true;
public function Circle()
{
this.x = 100;
this.y = 200;
vector = new Point(1, 1);
draw();
}
private function draw(): void {
graphics.clear();
graphics.beginFill(0x0000FF);
graphics.drawCircle(0, 0, 50);
graphics.endFill();
addEventListener(Event.ENTER_FRAME, Update);
prevTime = getTimer();
}
public function Update(e:Event): void {
//ball speed
this.x += speed * vector.x * ((getTimer() - prevTime) / 1000);
this.y += speed * vector.y * ((getTimer() - prevTime) / 1000);
//ball wall hit limits
if (this.x < radius) {
hit = true;
vector.x = 1;
}
if (this.x > this.stage.stageWidth - radius) {
hit = true;
vector.x = -1;
}
if (this.y < radius) {
hit = true;
vector.y = 1;
}
if (this.y > this.stage.stageHeight - radius) {
this.parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, Update);
GameGlobals.gameover = true;
//GameGlobals.Instance.MigsCircle = null;
var index:int = GameGlobals.Instance.MigsCircleArr.indexOf(this);
GameGlobals.Instance.MigsCircleArr.splice(index, 1);
for (var i:int = 0; i < GameGlobals.Instance.MigsCircleArr.length; i++) {
var currCircle:Circle = GameGlobals.Instance.MigsCircleArr[i];
//do remove circle in list
this.removeEventListener(Event.EXIT_FRAME, Update);
}
}
if (this.hitTestObject(GameGlobals.Instance.MigsPaddle)) {
if (hit == true) {
GameGlobals.Instance.score++;
hit = false;
}
vector.y = -1; //when ball hits paddle, reverse y axis direction
if (GameGlobals.Instance.score > GameGlobals.Instance.highscore) {
GameGlobals.Instance.highscore = GameGlobals.Instance.score;
}
}
prevTime = getTimer();
}
public function set Diameter(num: Number): void
{
radius - num / 2;
}
public function get Diameter(): Number
{
return radius * 2;
}
//angle Getters and Setters
private var _angle: int;
public function set angle(num: Number): void
{
if (_angle > 180)
_angle = 180;
else if (_angle < -180)
_angle = -180;
}
public function get angle(): Number
{
return _angle;
}
}
}
CircleSpawner (the class that makes the game make new balls appear in a set interval:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getTimer;
/**
* ...
* @author Migs Rebueno
*/
public class CircleSpawner extends Sprite
{
public var startTime: int = 0;
public var spawnIntervalTime: Number = 2.0;
public function CircleSpawner()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void {
startTime = getTimer() / 1000;
addEventListener(Event.ENTER_FRAME, update)
}
public function update(e:Event):void {
if ((getTimer() / 1000) - startTime > spawnIntervalTime) { //add a new circle every two seconds
var myCircle:Circle = new Circle();
this.parent.addChild(myCircle);
GameGlobals.Instance.MigsCircleArr.push(myCircle);
startTime = getTimer() / 1000;
}
}
}
}
The paddle class (I called this class Paddlez just for my own fun):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.text.engine.JustificationStyle;
import flash.utils.getTimer;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
/**
* ...
* @author Migs Rebueno
*/
public class Paddlez extends Sprite
{
public var keyboard:Array;
public var location: Point = new Point();
public var len: Number = 100;
public var wid: Number = 30;
public var vector:Point = new Point(1, 1);
public var speed: Number = 400;
public var prevTime: Number;
public function Paddlez()
{
draw();
this.addEventListener(Event.ADDED_TO_STAGE, init);
//addEventListener(KeyboardEvent.KEY_UP, onkeyup);
}
public function init(e:Event):void {
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onkeydown);
}
private function draw(): void {
graphics.clear();
graphics.beginFill(0x0000FF);
graphics.drawRect(375, 550, len, wid);
graphics.endFill();
prevTime = getTimer();
}
public function onkeydown(e:KeyboardEvent):void {//padle movement by keyboard
switch (e.keyCode) {
case (Keyboard.LEFT):
this.x -= 25;
break;
case (Keyboard.RIGHT):
this.x += 25;
break;
}
if (this.x < -375) {
this.x = -375;
}
if (this.x > 325) {
this.x = 325
}
}
public function onkeyup(e:KeyboardEvent):void {
e.keyCode == Keyboard.RIGHT;
e.keyCode == Keyboard.LEFT;
}
}
}
Game class (which is basically the main game class itself):
package
{
import flash.display.Sprite;
import Circle;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import Paddlez;
/**
* ...
* @author Migs Rebueno
*/
public class Game extends Sprite
{
public var Score_Text:TextField;
public var High_Score:TextField;
public function Game()
{
//GameGlobals.Instance.MigsCircle = new Circle();
GameGlobals.Instance.MigsPaddle = new Paddlez();
//addChild(GameGlobals.Instance.MigsCircle);
addChild(GameGlobals.Instance.MigsPaddle);
addChild(new CircleSpawner());
Score_Text = new TextField();
Score_Text.text = "Score: " + GameGlobals.Instance.score;
addChild(Score_Text);
High_Score = new TextField();
High_Score.text = "High Score: " + GameGlobals.Instance.highscore;
High_Score.width = 100;
High_Score.y = 12;
addChild(High_Score);
addEventListener(Event.ENTER_FRAME, Update);
}
public function Update(e:Event):void {
if (GameGlobals.Instance.MigsCircle == null) {
GameGlobals.Instance.MigsCircle = new Circle();
addChild(GameGlobals.Instance.MigsCircle); //respawn the circle when it lands
GameGlobals.Instance.score = 0; //when paddle does not hit ball and when it hits the lower side, reset score to 0
if (GameGlobals.Instance.MigsCircle.y == 1) {//remove circles if one ball hits the floor
removeChild(GameGlobals.Instance.MigsCircle);
removeEventListener(Event.EXIT_FRAME, Update);
}
}
Score_Text.text = "Score: " + GameGlobals.Instance.score;
if (hitArea == true) {
GameGlobals.Instance.highscore++;
hitArea == false;
}
High_Score.text = "High Score: " + GameGlobals.Instance.highscore;
}
}
}
The GameGlobals class which contains global elements for the game:
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import Circle;
import Paddlez;
/**
* ...
* @author Migs Rebueno
*/
public class GameGlobals extends Sprite
{
public static var gameover:Boolean = false;
private static var _instance:GameGlobals = null;
public var MigsCircle:Circle;
public var MigsPaddle:Paddlez;
public var score:int = 0; //starting point
public var highscore:int; //high score
public var keyboard:Array = new Array();
public var MigsCircleArr = new Array();
public function GameGlobals() {
}
public static function get Instance():GameGlobals {
if (_instance == null) {
_instance = new GameGlobals();
}
return _instance;
}
public function keyup(e:KeyboardEvent):void {
keyboard[e.keyCode] = false;
}
public function keydown(e:KeyboardEvent):void {
keyboard[e.keyCode] = true;
}
}
}
(Added this class but it isn't necessary, which is called GlobalConst:
package
{
/**
* ...
* @author Migs Rebueno
*/
public class GlobalConst
{
public static const GRAVITY:Number = 9.8;
public static function square(i:Number):Number
{
return i * i;
}
}
}
)Anyway, the only problem now is that my pong game is in an infinite loop where, when the ball lands, the score will keep increasing and balls will just keep spawning in... I have no errors on this though...
I have to make something that will make the balls disappear if one of them goes past the paddle. What should I put here?
Again, I am not asking anybody who will be replying here to do this for my own sake... I am just seeking the answers...

New Topic/Question
Reply


MultiQuote


|