[問題] 在global和function中有差別嗎

看板Flash作者 (就這樣吧)時間15年前 (2010/10/03 19:42), 編輯推噓0(006)
留言6則, 2人參與, 最新討論串1/1
不好意思 每天都來一問 @@" 在function中除了存取區域變數的差別外 還有什麼差別嗎 因為我有一段讓元件移動的code 寫在global中就可以運作 寫在function中讓event trigger去執行這function 元件就不會移動了 基本上有一個array(名稱mainMenuList)裡面放數個要移動的元件實體名稱 因此this[mainMenuList[index]]就是元件的實體 先讓他們到指定的位置 然後用motion去替每一個算出它的移動路線 再用animFactory去執行, 再放入array中 接著呼叫array中的object讓他們開始移動 編譯和執行都沒有錯誤 不過放到這個function裡面 就不會移動了 請問是this的關係嗎? 那部份的code如下 function afterCenterMotion(event:MotionEvent):void { //misc var i:int = 0; //Show main menu var rotateRadius:int = center.width / 2 + this[mainMenuList[0]].height / 2 + 10; for (i = 0; i< mainMenuList.length; i++) { this[mainMenuList[i]].x = center.x; this[mainMenuList[i]].y = center.y - rotateRadius; } var motionArray:Array = new Array(); for (i = 0; i<mainMenuList.length; i++) { var rotateAngle:int = 360/mainMenuList.length*(mainMenuList.length-i); var motionDuration = 24; var anglePerFrame:int = 360 / motionDuration; var motionX:Array = new Array(); var motionY:Array = new Array(); var motion:Motion; for (var j = 0; j<motionDuration; j++) { motionX[j] = Math.sin(j * anglePerFrame * Math.PI / 180) * rotateRadius; motionY[j] = (1-Math.cos(j*anglePerFrame* Math.PI / 180 ))*rotateRadius; if ( (j+1)/(motionDuration/mainMenuList.length) >= i+1) { break; } } motion = new Motion(); motion.duration = motionDuration; motion.addPropertyArray("x",motionX); motion.addPropertyArray("y",motionY); var animFactory = new AnimatorFactory(motion); animFactory.transformationPoint = new Point(0.500000,0.500000); motionArray.push(animFactory); } for (i = 0; i<mainMenuList.length; i++) { motionArray[i].addTarget(this[mainMenuList[i]], 1); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 124.219.8.122 ※ 編輯: famet 來自: 124.219.8.122 (10/03 19:44)

10/04 01:02, , 1F
不好意思 我測了一下 好像和this沒關係
10/04 01:02, 1F

10/04 01:03, , 2F
是motion和animfactory在function裡沒作用
10/04 01:03, 2F

10/04 01:03, , 3F
請問有人知道怎麼解決嗎?
10/04 01:03, 3F

10/04 12:57, , 4F
我找到問題了 我把animfactory宣告在function外面就可以了
10/04 12:57, 4F

10/04 12:57, , 5F
雖然我還是不知道為什麼= =
10/04 12:57, 5F

10/04 14:56, , 6F
function內宣告的東西function外面是抓不到的
10/04 14:56, 6F
文章代碼(AID): #1Cg6msd4 (Flash)