Im new to Flash AS3 Game programming,,,There is a problem i'm facing,,,Im trying to create an opening interface(menu with play button),,,during game play the player gives input through keyboard,,,,and game should jump to frame 10(to show some object) and then reset back to frame1. Now when my game resets back to the frame 1,,,the dynamically removed menu at the begining reapears,,,,,, Part of my code with the problem is given below....To run the code just add some temp library object tat at frame 1 and allow access to it through as3. also add a keyframe at frame 10.开发者_运维技巧
var keyPressed:uint;
var rightkey:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, playshot);
var timer:Timer = new Timer(3700, 1); //create the timer
timer.addEventListener(TimerEvent.TIMER, initializeGame);
var tmenu:tat = new tat(); //show a start menu with play button
addChild(tmenu); //added to the stage
tmenu.addEventListener(MouseEvent.CLICK,controlmenu);
function controlmenu(event:MouseEvent) {
timer.start();
removeChild(tmenu); //when the play button is clicked //button and start the game
}
function playshot(event:KeyboardEvent):void { //detects the keys pressed and shots accordingly {
keyPressed = event.keyCode;
if(keyPressed == Keyboard.RIGHT)
{
trace("Entering movebat function");
gotoAndStop(10); //show a show rectangle shape at frame 10;
timer.start();
}
}
function initializeGame(e:TimerEvent):void
{
gotoAndStop(1);
}
stop();
If you jump to a frame that has code on it, that code will be executed again. You are likely putting your code at that first frame, so when you make it jump back there the menu adding code is run again. Put your initializing code on a frame you only hit once, or even better, use a document class and avoid skipping around the timeline altogether.
精彩评论