我是as3的新手。而我正在努力解决错误1119.我不明白如何解决它。 错误是:
通过静态类型flash.text:TextField。
的引用访问可能未定义的属性文本package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
// public function kan je ook gebruiken in een ander as. file
// private function kan je alleen gebruiken in hetzelfde as. file
public class firstGame extends MovieClip
{
public var mcPlayer:MovieClip;
private var leftKeyIsDown:Boolean;
private var rightKeyIsDown: Boolean;
private var aMissileArray: Array;
private var aEnemyArray: Array;
public var scoreTxt: TextField;
public var ammoTxt : TextField;
public var menuEnd: mcEndGameScreen;
private var tEnemyTimer : Timer;
private var nScore : Number;
private var nAmmo : Number;
public function firstGame ()
{
playGameAgain(null);
}
private function playGameAgain (e:Event) : void
{
//initilaiz variables
aMissileArray = new Array ();
aEnemyArray = new Array ();
nScore = 0;
nAmmo = 20;
mcPlayer.visible = true;
updateScoreText();
updateAmmoText();
menuEnd.addEventListener("PLAY_AGAIN", playGameAgain);
menuEnd.hideScreen ();
//trace("First Game Loaded");
//Listern for key presses and relesead
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
//Setup game event loop
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
// create a timer object
tEnemyTimer = new Timer (1000);
// listener for timer intervals
tEnemyTimer.addEventListener(TimerEvent.TIMER, addEnemy);
// start out timer
tEnemyTimer.start();
}
private function updateScoreText (): void
{
scoreTxt.txt = "Score: " + nScore;
}
private function updateAmmoText (): void
{
ammoTxt.txt = "Ammo: " + nAmmo;
}
private function addEnemy (e:TimerEvent) : void
{
//trace ("timer ticks")
// create a new enemy object
var newEnemy:mcEnemy = new mcEnemy ();
// add object to the stage
stage.addChild (newEnemy);
// add enemy to new enemy to a new enemy array
aEnemyArray.push(newEnemy);
trace (aEnemyArray.length);
}
private function gameLoop (e:Event) : void
{
playerControl();
clampPlayerToStage();
checkMissileOffscreen();
checkEnemiesOffscreen();
checkMissilesHitsEnemy();
checkEndGameCondition();
}
private function chechEndGameCondition (): void
{
//check if player has 0 missiles left and 0 on screen
if ( nAmmo == 0 && aMissileArray.length == 0)
{
// stop player movement
stage.removeEventListener(KeyboardEvent.KEY_UP, KeyUp);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
// hide the player
mcPlayer.visible = false;
// stop spawing enemies
tEnemyTimer.stop();
// clear enemies on screen
for each ( var enemy: mcEnemy in aEnemyArray)
{
// destroy the enemy
enemy.destroyEnemy();
// remove from enemy array
aEnemyArray.splice( 0,1);
}
// stop the game loop
if(aEnemyArray.length == 0 )
{
stage.removeEventListener(Event.ENTER_FRAME, gameLoop);
}
// show our endgame screen
menuEnd.showScreen();
}
}
private function checkMissilesHitsEnemy (): void
{
// loop trough current missiles
for (var i : int = 0 ; i < aMissileArray.length; i++)
{
// get our current missile in the i loop
var currentMissile : mcMissile = aMissileArray [i];
// loop trough all our enemies
// gebruik geen i want die is al gebruikt dus j
for (var j: int = 0 ; j < aEnemyArray.length; j++)
{
// get the current enemy in the j loop
var currentEnemy: mcEnemy = aEnemyArray [j];
// test if our current enemy is hitting our current missile
if(currentMissile.hitTestObject(currentEnemy))
{
// create an explosion
// create a new explosion instance/movielcip
var newExplosion: mcExplosion = new mcExplosion ();
// add our explosion to the stage
stage.addChild( newExplosion)
// position our expolosion to our enemy
newExplosion.x = currentEnemy.x;
newExplosion.y = currentEnemy.y;
// remove the missile
currentMissile.destroyMissile();
// remove the missile from missile array
aMissileArray.splice(i, 1);
// remove the enemy from the stage
currentEnemy.destroyEnemy();
// remove the enemy from the enemy array
aEnemyArray.splice(j, 1);
// add one to our score
nScore++;
updateScoreTxt ();
}
}
}
}
private function checkEnemiesOffscreen (): void
{
// loop trough all our enemies
for (var i:int = 0;i < aEnemyArray.length; i++)
{
// get our current ememy in the loop
var currentEnemy: mcEnemy = aEnemyArray [i];
// when enemy moves left and is has gone past the and of the left from the stage
if (currentEnemy.sDirection == "L" && currentEnemy.x - (currentEnemy.width/2))
{
// Remove enemy from our array
aEnemyArray.slice(i,1);
// Remove enemy from stage
currentEnemy.destroyEnemy();
} else
if (currentEnemy.sDirection == "R" && currentEnemy.x > stage.stageWidth + (currentEnemy.width/2))
{
// Remove enemy from our array
aEnemyArray.slice(i,1);
// Remove enemy from stage
currentEnemy.destroyEnemy();
}
}
}
private function checkMissileOffscreen():void
{
//Loop throw all our missiles in our missle array
// i = counter object
for (var i: int = 0; i < aMissileArray.length; i++)
{
//Get the current missile in the loop
var currentMissile : mcMissile = aMissileArray [i];
//Test if current missile is out the buttom of the screen
if (currentMissile.y > 450 )
{
//Remove current missile from the array
aMissileArray.splice(i,1);
//Destroy our missile
currentMissile.destroyMissile();
}
}
}
private function clampPlayerToStage ():void
{
// if our player is to the left of the stage
if (mcPlayer.x < (mcPlayer.width/2))
{
// set our player to left of the stage
mcPlayer.x = mcPlayer.width/2;
}
// if our player is to the right of the stage
else if (mcPlayer.x > (stage.stageWidth - (mcPlayer.width/2)))
{
//set our player to right of the stage
mcPlayer.x = stage.stageWidth - (mcPlayer.width/2);
}
}
private function playerControl ():void
{
// if our left key is down currently
if (leftKeyIsDown == true)
{
//move to left
mcPlayer.x -= 5;
}
// if our right key is currently down
if (rightKeyIsDown)
{
//move to right
mcPlayer.x += 5;
}
}
private function keyUp (e:KeyboardEvent): void
{
//trace(e.keyCode)
//if your left is released
if (e.keyCode == 37)
{
//left key is released
leftKeyIsDown = false;
}
//if your right is released
if (e.keyCode == 39)
{
//right key is released
rightKeyIsDown = false;
}
//if our spacebarr is released
if (e.keyCode == 32)
{
// test if player has missiles left
if (nAmmo > 0)
{
nAmmo--;
updateAmmoTxt();
//fire a missile
fireMissile ();
}
}
}
private function fireMissile ():void
{
// create a new missisile object
var newMissile : mcMissile = new mcMissile ();
// add to stage
stage.addChild(newMissile);
// position missile
newMissile.x = mcPlayer.x;
newMissile.y = mcPlayer.y;
//add our new missile to our missile array
aMissileArray.push (newMissile);
trace(aMissileArray.length)
}
private function keyDown (e:KeyboardEvent): void
{
//trace(e.keyCode)
//if your left is pressed
if (e.keyCode == 37)
{
//left key is pressed
leftKeyIsDown = true;
}
//if your right is pressed
if (e.keyCode == 39)
{
//right key is pressed
rightKeyIsDown = true;
}
}
}
}
如果有人可以帮我获得scoreTxt,我想我可以自己做ammoTxt。
答案 0 :(得分:0)
它的拼写错误scoreTxt.txt
应为scoreTxt.text
。
错误1119说Access of possibly undefined property <propertyname> through a reference with static type <typename>.
这意味着您的某些代码使用了一个可能未初始化的对象。在您的情况下,它是scoreTxt
,正如您已经想到的那样。
要查看它崩溃的原因,您需要手动完成程序。我更喜欢取消注释整个代码块并慢慢缩小问题,但这里不需要。
首先,点击文档类的构造函数firstGame()
。
这反过来点击playGameAgain()
,带有null
参数。
该函数运行一些代码......然后它命中updateScoreText()
。它崩溃了。这给出了以下调用堆栈和执行代码:
调用堆栈:
updateScoreText()
from playGameAgain(null) on line "updateScoreText();"
from firstGame() on line "playGameAgain(null);"
执行代码:
playGameAgain(null);
//leads to...
//initilaiz variables
aMissileArray = new Array ();
aEnemyArray = new Array ();
nScore = 0;
nAmmo = 20;
mcPlayer.visible = true;
updateScoreText();
//leads to...
scoreTxt.txt = "Score: " + nScore;//crash
您需要先链接或创建scoreTxt
。它现在可能是null
或undefined
,这是造成1119错误的原因。要链接scoreTxt
以便您的文档类可以找到它,您应该为它们提供实例名称。如果我是你,我会验证我的实例名称。
您还必须为ammoTxt
执行此操作。