![](/img/trans.png)
[英]Action Script 3.0 The public attribute can only be used inside a package
[英]Action Script 3.0 Mouse Event in a class package
在類中使用鼠標單擊事件時遇到問題,我絕對是Action Script的初學者。
我想要的是,如果我單擊btn_MClick按鈕,它應該運行腳本,但是每次我單擊它時,都會收到錯誤消息,即btn_MClick是未定義的。
btn_MClick處於舞台上,並且實例名稱為btn_MClick
public class gunShip1 extends MovieClip
{
var moveCount = 0;
public function gunShip1()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveGunShip1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, ShootGunShip1)
btn_MClick.addEventListener(MouseEvent.MOUSE_DOWN.KEY_DOWN, ShootGunShip1);;
}
function ShootGunShip1(evt: MouseEvent)
{
var s_Bullet:survBullet = new survBullet();
var stagePos:Point = this.localToGlobal (new Point(this.width / 2-10, this.height));;
s_Bullet.x = stagePos.x;
s_Bullet.y = stagePos.y;
parent.addChild(s_Bullet);
//play sound
var gun_sound:ricochetshot = new ricochetshot();
gun_sound.play();
}
}
拜托,我絕對不知道該怎么做,而且感覺整個過程是錯誤的。
您的gunShip1
類不具有btn_MClick
屬性,而root
或document類具有。
基本上,發生的事情是您將按鈕放在了舞台上,這使其成為一個屬於根容器的實例。 目前,您正在嘗試將按鈕引用為gunShip1
的屬性。
在這里,您真正應該做的是單獨對gunShip1
進行按鈕單擊管理,並使該單獨的代碼調用gunShip1
方法。 例如,您可以在文檔類中包含以下內容:
public class Game extends MovieClip
{
private var _ship:gunShip1;
public function Game()
{
_ship = new gunShip1();
// The Document Class will have reference to objects on the stage.
btn_MClick.addEventListener(MouseEvent.CLICK, _click);
}
private function _click(e:MouseEvent):void
{
_ship.shoot();
}
}
然后在gunShip1
更新shoot
方法:
public function shoot():void
{
var s_Bullet:survBullet = new survBullet();
var stagePos:Point = this.localToGlobal (new Point(this.width / 2 - 10, this.height));
s_Bullet.x = stagePos.x;
s_Bullet.y = stagePos.y;
parent.addChild(s_Bullet);
var gun_sound:ricochetshot = new ricochetshot();
gun_sound.play();
}
這個想法是, gunShip1
不應該負責處理用戶輸入(鼠標,鍵盤等)。 而是應該是一個單獨的類,該類通知gunShip1
它應該做某事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.