我對ActionScript 3很新,但我一直在逐步構建一個小型的「坦克」式射擊遊戲,現在我正在添加我的子彈射擊要做到這一點我做了一個新的AS3類並將其導入等,但是當我運行遊戲出現錯誤:AS3 - 「調用一個可能未定義的方法」
Call to a possibly undefined method
turretShot
請幫我找出爲什麼出現這種情況。這個錯誤在我的編譯器錯誤兩次展示,它是對線非常相似,這裏是代碼的部分影響:
shootTurret.as(.class文件)
package resources
{
import flash.display.*;
import flash.events.*;
public class turretShot extends MovieClip
{
var enemy_mc:MovieClip;
public function turretShot(enemy_mc:MovieClip)
{
var xSpeed:Number;
var ySpeed:Number;
var angle:Number;
public var shotSpeed:Number;
speed = 30;
var gravity:Number = 1;
var thisShot:MovieClip;
var platform1_mc:MovieClip;
var platform2_mc:MovieClip;
var thisParent: *;
enemy_mc = enemy;
this.addEventListener(Event.ADDED, initialize);
}
function initialize(event:Event):void
{
this.addEventListener(Event.ENTER_FRAME, moveShot);
thisParent = event.currentTarget.parent;
platform1_mc = thisParent.platform1_mc;
platform2_mc = thisParent.platform2_mc;
thisShot = MovieClip(this.parent.getChildByName(this.name));
angle = this.rotation/180 * Math.PI;
xSpeed = Math.cos(angle) * speed;
ySpeed = Math.sin(angle) * speed;
}
function moveShot(event:Event):void
}
}
我在今年進口我遊戲.FLA動作腳本所需的幀:
import resources.*;
因爲我的類文件位於遊戲目錄的「resources」文件夾中。
現在,代碼的兩個節都扔這些錯誤......不知道它是什麼與他們或只是類,但他們:
(4號線)
var shot_mc:MovieClip;
if (keyPressed == Keyboard.SPACE && spaceKeyDown == false)
{
shot_mc = new turretShot(enemy_mc);
shot_mc.x = user_mc.x + user_mc.turret_mc.x;
shot_mc.y = user_mc.y + user_mc.turret_mc.y;
shot_mc.rotation = user_mc.turret_mc.rotation;
addChild(shot_mc);
spaceKeyDown = true;
}
和(第一行):
var shot_mc:MovieClip = new turretShot(user_mc);
shot_mc.x = enemy_mc.x + enemy_mc.enemyTurret_mc.x;
shot_mc.y = enemy_mc.y + enemy_mc.enemyTurret_mc.y;
shot_mc.rotation = enemyTargetRotation;
shot_mc.speed = Math.random() * 15 + 15;
addChild(shot_mc);
任何幫助,將不勝感激。
感謝您的回覆,除了我描述的兩個編譯器錯誤之外沒有任何顯示,它的目的是爲了「public function turretShot(enemy_mc:MovieClip)」我只是錯誤地鍵入了enemy = enemy_mc;錯誤的方式,我解決了這個問題,仍然得到了與我的代碼相同的2個錯誤,我的代碼的修復將不勝感激,我真的很新,因此很難確定該怎麼做。 – user1637318
如果編譯器不會吐出有關此類的錯誤,那麼可能是,正如The_asMan所暗示的,您沒有將它導入到主腳本中。這樣做可以幫助您查明此代碼中大部分剩餘的錯誤。 –
@ the-asman再次感謝您試圖提供幫助,但仍然...同樣的兩個錯誤,我已經用「import resources。*;」導入了它。它導入資源目錄中的每個AS文件,即使現在我已經將目標鎖定在精確的類文件上,也不例外。真的在這裏掙扎,需要得到這個工作:( – user1637318