View Full Version : Problem moving code from timeline to doc class
spike50
July 30th, 2008, 04:48 PM
Hi,
I am pretty new to AS. I'm trying to move some working AS3 code from the main timeline to a doc class but am getting a series of error messages. Can anyone tell me what I'm doing wrong? I'm attaching the FLA and AS files. The AS has my code attempt at the top and the original code (from the timeline) commented out at the bottom. Thanks for any help!
M
stringy
July 30th, 2008, 05:37 PM
I think this works the same as the fla
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
public class Game extends MovieClip {
private var monstersInGame:uint;
private var monsterMaker:Timer;
private var container_mc:MovieClip;
private var cursor:MovieClip;
private var score:int;
private var energy:int;
public function Game() {
initializeGame();
}
public function initializeGame():void {
monstersInGame = 10;
monsterMaker = new Timer(1000, monstersInGame);
container_mc = new MovieClip();
addChild(container_mc);
monsterMaker.addEventListener(TimerEvent.TIMER, createMonsters);
monsterMaker.start();
cursor = new Cursor();
addChild(cursor);
cursor.enabled = false;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
score = 0;
energy = energy_mc.totalFrames;
energy_mc.gotoAndStop(energy);
}
public function dragCursor(event:MouseEvent):void {
cursor.x = this.mouseX;
cursor.y = this.mouseY;
}
public function createMonsters(event:TimerEvent):void {
var monster:MovieClip;
monster = new Monster();
monster.x = Math.random() * stage.stageWidth;
monster.y = Math.random() * stage.stageHeight;
container_mc.addChild(monster);
}
public function increaseScore():void {
score ++;
if (score >= monstersInGame) {
monsterMaker.stop();
showWinLose("you win!");
}
}
public function decreaseEnergy():void {
energy --;
if (energy <= 0) {
monsterMaker.stop();
showWinLose("you lose!");
} else {
energy_mc.gotoAndStop(energy);
}
}
public function showWinLose(endMessage:String):void {
var winLose:MovieClip = new WinLose();
addChild(winLose);
winLose.end_txt.text = endMessage;
}
}
}
spike50
July 30th, 2008, 05:49 PM
Stringy, Thank you so much! From quickly looking over your code changes, it looks like my main problem was not importing all the needed elements. Thanks again!
M
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.