我有一段艰难的时间试图解决这个问题。这是我自己的第一个游戏的代码,无论何时我运行它返回的程序:
场景1,图层'动作脚本',第2帧,第95行,第36列1067: 类型Class的值隐式强制到不相关的类型 flash.display使用:。的DisplayObject
我试图在任何地方切换东西,我无法弄清楚如何解决它。浏览我注意到有关混合实例和类的内容,但是我对此非常新,并且不确定它们之间的区别以及从何处开始解决该问题。以下是我的全部代码。
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
stop();
var container:MovieClip = new MovieClip;
addChild(container);
//Make the player move
function playerMovement (event:Event):void
{
playerMC.y = stage.mouseY
}
stage.addEventListener(Event.ENTER_FRAME, playerMovement);
//-----
//Make playerLasers work
function playerShoot (event:MouseEvent):void
{
var playerLaser:MovieClip = new playerLaserMC;
playerLaser.y = playerMC.y + 69;
playerLaser.x = playerMC.x + 125;
container.addChild(playerLaser);
}
stage.addEventListener(MouseEvent.CLICK, playerShoot);
//add damage dealt here -
//--------
//Spawn random enemies
function addEnemy(event:Event)
{
var sendEnemy:Number = Math.random();
if (sendEnemy<=.01)
{
var enemyGenerator:Number = Math.random();
var minY:uint = 0;
var maxY:uint = 300;
var rangeY = maxY - minY;
var randomY:Number = Math.ceil(Math.random()*rangeY) + minY
}
if (enemyGenerator <=.25)
{
var newEnemy1:MovieClip = new enemyType1();
newEnemy1.x = 100;
newEnemy1.y = randomY;
container.addChild(newEnemy1);
}
else if(enemyGenerator>.25, enemyGenerator<=.5)
{
var newEnemy2:MovieClip = new enemyType2();
newEnemy2.x = 100;
newEnemy2.y = randomY;
container.addChild(newEnemy2);
}
else if(enemyGenerator>.5, enemyGenerator<=.75)
{
var newEnemy3:MovieClip = new enemyType3();
newEnemy3.x = 100;
newEnemy3.y = randomY;
container.addChild(newEnemy3);
}
else if(enemyGenerator>.75, enemyGenerator<=1)
{
var newEnemy4:MovieClip = new enemyType4();
newEnemy4.x = 200;
newEnemy4.y = randomY;
container.addChild(newEnemy4);
}
}
stage.addEventListener(Event.ENTER_FRAME, addEnemy);
//--------
//Destoy Enemies
function destroyEnemy(event:Event):void
{
if (container.hitTestObject(enemyType1))
{
trace ("Hit Enemy");
}
else if (container.hitTestObject(enemyType2))
{
trace ("Hit Enemy");
}
else if (container.hitTestObject(enemyType3))
{
trace ("Hit Enemy");
}
else if (container.hitTestObject(enemyType4))
{
trace ("Hit Enemy");
}
}
stage.addEventListener(Event.ENTER_FRAME, destroyEnemy);
答案 0 :(得分:0)
enemyType1
是class
而非variable
,因此您无法写信:
if (container.hitTestObject(enemyType1)) {}
您已创建名为enemyType1
的类newEnemy1
的实例:
var newEnemy1:enemyType1 = new enemyType1();
newEnemy1.x = 100;
newEnemy1.y = randomY;
container.addChild(newEnemy1);
你通过变量调用这个实例:
if (container.hitTestObject(newEnemy1)) {}
答案 1 :(得分:0)
我做了一个类似的例子,问题就解决了。但是,这样做的问题是,如果在newEnemy1
中添加container
,则会重复发生hitTestObject
循环的事件。运行程序时,您会看到输出窗口具有
follow..
Hit Enemy
Hit Enemy
Hit Enemy
Hit Enemy
Hit Enemy
...
因为playerLaser
和newEnemy1
都在container
中。
我这样解决了这个问题:
替换此行...
container.addChild(newEnemy1);
使用
addChild(newEnemy1);
我希望这可以为您提供帮助。