2012-07-01 133 views
0

最近,我開始編程時,我有一個移動功能問題,該劑量沒有表現出運動,無物體移動動畫

我使用的代碼:

function cardmove(card){ 
    xDiff=(playerHand+space+card.width)-card._x; 
    yDiff=(Stage.height/2+space)-card._y; 
     speedx =xDiff/10; 
     speedy =yDiff/10; 
    for (frame=0;frame<10;frame++){ 
     card._x+=speedx; 
     card._y+=speedy;} 
    playerHand = card._x ; 
} 

卡到位它應該在開始時是正確的,並且不顯示移動。

回答

1

Flash在循環期間不會刷新佈局。 如果您想移動某物,則必須在兩步之間將手放回閃光燈。

你必須做的兩個主要方式,setInterval的:

// You need to declare this variables to access them in the function "applyMovement" 
var _nInterval:Number; 
var _nSteps:Number; 

// Yout function 
function cardmove(card){ 
    xDiff=(playerHand+space+card.width)-card._x; 
    yDiff=(Stage.height/2+space)-card._y; 
    speedx =xDiff/10; 
    speedy = yDiff/10; 

    // Set the steps to 10 
    _nSteps = 10; 
    // Before start the setInterval, you have to stop a previous one 
    clearInterval(_nInterval); 
    // You call the function each 41 ms (about 24 fps) with the parameters 
    setInterval(this, "applyMovement", 41, card, speedx, speedy); 
} 
// This function apply the new coodinates 
function applyMovement(mc:MovieClip, nX:Number, nY:Number) { 
    if (_nSteps >= 0) { 
     mc._x += nX; 
     mc._y += nY; 
    }else { 
     clearInterval(_nInterval); 
    } 
    _nSteps --; 
} 

或的onEnterFrame:

// You need to declare this variables to access them in the function "applyMovement" 
var _nSteps:Number; 
var _mc:MovieClip; 
var _nX:Number; 
var _nY:Number; 

// Yout function 
function cardmove(card){ 
    xDiff=(playerHand+space+card.width)-card._x; 
    yDiff=(Stage.height/2+space)-card._y; 
    _nX =xDiff/10; 
    _nY = yDiff/10; 

    _mc = card; 

    // Set the steps to 10 
    _nSteps = 10; 
    // You have to use the Delegate function to access to the "good" this in the function 
    this.onEnterFrame = mx.utils.Delagate.create(this,applyMovement); 
} 
// This function apply the new coodinates 
function applyMovement() { 
    if (_nSteps >= 0) { 
     _mc._x += nX; 
     _mc._y += nY; 
    }else { 
     delete this.onEnterFrame; 
    } 
    _nSteps --; 
}