PDA

View Full Version : [AS3] Trying to get enemy spawn


Corr500
01-09-2011, 10:06 PM
Hey I am very new to actionscript and making a basic fruit catcher game. Trying to get enemy spawn, know the code I have works for a friend, but in mine my file will load with no errors. Want my fruit to fall down the screen, but when it loads, nothing is there and nothing happens. My code:

Main:
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.TextEvent;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.ui.Mouse;
import classes.*;// << imports all of the class files inside classes folder

public class Main extends Sprite {
public var enemy:Array;
private var A_apple:AC_apple;


private var timeRemaining:Date = new Date();
private var countDownTimer:Timer;

public function Main():void {
play_button.visible=true;
goBox.visible=false;
enemy = new Array();

var format:TextFormat = new TextFormat();
format.font="Times new Roman";
format.color=0xFF0000;
format.size=25;
format.leading=4;

timeBox = new TextField() ;
timeBox.defaultTextFormat=format;
timeBox.x=stage.stageWidth/2+20;// put it next to the static prompt text
timeBox.y=186;
timeBox.width=60;
timeBox.height=30;
timeBox.border=timeBox.background=false;//no border or background colour
timeBox.type=TextFieldType.INPUT;// allow user input
timeBox.maxChars=2;
timeBox.restrict="0-9";
stage.addChild(timeBox);
stage.focus=timeBox;



timeBox.addEventListener(KeyboardEvent.KEY_DOWN, startGame);
}


private function startGame(event:KeyboardEvent):void {
if (event.keyCode==Keyboard.ENTER) {
this.timeRemaining.time=int(timeBox.text)*1000;
play_button.visible=false;
timeBox.visible=false;
stage.removeEventListener(KeyboardEvent.KEY_DOWN,s tartGame);




countDown.text=convertTime(this.timeRemaining);

//Paul Vickers - textAndStuff
this.countDownTimer=new Timer(100,this.timeRemaining.time/100);
this.countDownTimer.addEventListener(TimerEvent.TI MER, tickTock);
this.countDownTimer.start();
this.countDownTimer.addEventListener(TimerEvent.TI MER_COMPLETE, endGame);
}
}
public function onTick(timerEvent:TimerEvent):void {

var newApple = new AC_apple (200, 15);
enemy.push(newApple);
addChild(newApple);

for each (var A_apple:AC_apple in enemy) {
A_apple.moveDownABit();
}


}

//Paul Vickers - textAndStuff
private function tickTock(e:TimerEvent) {
this.timeRemaining.time-=100;
this.countDown.text=convertTime(this.timeRemaining );
}

//Paul Vickers - textAndStuff
private function convertTime(t:Date):String {
var mm:String,ss:String,ms:String;
mm=t.minutes<10?"0"+t.minutes:t.minutes.toString();
ss=t.seconds<10?"0"+t.seconds:t.seconds.toString();

return mm+":"+ss
}

private function endGame(e:TimerEvent) {
countDown.visible=false;
goBox.visible=true;
}
}

}

Class Code:
package classes {

import flash.display.Sprite;



public class AC_apple extends Sprite {




// Initialization:
public function AC_apple(_x:uint, _y:uint) {
this.x = _x;
this.y = _y;
configUI();
}

public function moveDownABit():void
{
this.y = y + 3;
}


protected function configUI():void { }
}

}

There is alot of unnecessary code in their for my timer etc. Any help Much appreciated :D