我覺得你的問題是,你有你的時間表一幀以上,所以每次播放頭移動到另一幀時,腳本都會再次運行,一切都基本重置。
這是我會怎麼做(如果你願意嘗試一些更高級):
第一招確保沒有在舞臺上被選中,找到標Class:
場下的PUBLISH
標題
粘貼文字DocumentClass
。
現在和你的fla文件在同一個目錄下創建一個文件名DocumentClass.as
然後粘貼下面的內容,保存並運行fla文件。
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
// Start class
// This class must extend MovieClip
public class DocumentClass extends MovieClip{
private var acceleration:Number = 2;
private var friction:Number = 0.95;
// Limits the player's speed to prevent it from moving too fast
private var maxSpeed:Number = 6;
// The players velocity
private var playerSpeedX:Number = 0;
private var playerSpeedY:Number = 0;
// Keeps track of what keys are pressed
private var keyLeft:Boolean;
private var keyRight:Boolean;
// The contructor for DocumentClass - this is automatically called
public function DocumentClass(){
addEventListener(Event.ENTER_FRAME, eventEnterFrame, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_DOWN, eventKeyDown, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, eventKeyUp, false, 0, true);
}
// This function is called when a key is pressed
private function eventKeyDown(keyEvent:KeyboardEvent):void{
switch(keyEvent.keyCode){
// Left
case 37: keyLeft = true; break;
// Right
case 39: keyRight = true; break;
}
}
// This function is called when a key is released
private function eventKeyUp(keyEvent:KeyboardEvent):void{
switch(keyEvent.keyCode){
// Left
case 37: keyLeft = false; break;
// Right
case 39: keyRight = false; break;
}
}
// This function is called every frame.
// Move the player every frame instead of in the keyboard events.
private function eventEnterFrame(event:Event):void{
// If the left key is pressed and the player's speed is under the limit.
if(keyLeft && playerSpeedX > -maxSpeed)
playerSpeedX -= acceleration;
// Otherwise if the right key is pressed and the player's speed is under the limit.
else if(keyRight && playerSpeedX < maxSpeed)
playerSpeedX += acceleration;
playerSpeedX *= friction;
// I find that when the playerSpeedX values start to get really small the player
// will seems to just slowly move for a long so I clip the speed when it becomes very small
if(playerSpeedX < 0.1 && playerSpeedX > -0.1) playerSpeedX = 0;
player.x += playerSpeedX;
}
}// End class
}
當然,如果,在我修改原密碼一點點的那一刻有點超出你:
stage.addEventListener(KeyboardEvent.KEY_DOWN, eventKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, eventKeyUp);
addEventListener(Event.ENTER_FRAME, eventEnterFrame);
var power:Number = 5;
var friction:Number = 0.95;
var keyLeft:Boolean;
var keyRight:Boolean;
var xspeed:Number = 0;
var yspeed:Number = 0;
function eventKeyDown(keyEvent:KeyboardEvent):void{
// Left
if(keyEvent.keyCode==37){
keyLeft = true;
}
// Right
if(keyEvent.keyCode==39){
keyRight = true;
}
}
function eventKeyUp(keyEvent:KeyboardEvent):void{
// Left
if(keyEvent.keyCode==37){
keyLeft = false;
}
// Right
if(keyEvent.keyCode==39){
keyRight = false;
}
}
function eventEnterFrame(event:Event):void{
if(keyLeft) xspeed -= power;
if(keyRight) xspeed += power;
xspeed *= friction;
if(xspeed < 0.1 && xspeed > -0.1) xspeed = 0;
player.x += xspeed;
}
謝謝!這將幫助我學習as3更多:) – 2010-10-31 08:59:14