我制作了一个非常简单的“游戏”,其中创建了一个圆圈,当点击时,添加了一个新圆圈,点击新圆圈会添加另一个圆圈,同时点击任何旧圆圈“结束”游戏。我将有一个带有重启按钮的简单终局屏幕。如果代码很乱,我很抱歉,它适用于FlashDevelop中的练习。
问题是我试图在Main中调用我的“endGame”类函数但是我收到错误:“(58):col:5错误:参数数量不正确。预期1.”第58行是我对endGame()的调用
奇怪的是,即使endGame完全为空,我也会收到错误。我已经在下面发布了两个部分并在main中标记了(**)endGame调用。
package Fun
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Shape;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public var circles:Array;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
makeacircle()
}
public function makeacircle():void
{
circles = new Array();
var addcirc:addcircle;
addcirc = new addcircle((Math.random() * 999999))
circles.push(addcirc)
addChild(addcirc)
addcirc.x = 300
addcirc.y = 200
addEventListener(eventclass.LOOP, movecirc);
addcirc.addEventListener(MouseEvent.CLICK, transition )
function transition(e:MouseEvent):void
{
addcirc.removeEventListener(MouseEvent.CLICK, transition);
dispatchEvent ( new eventclass(eventclass.LOOP))
addcirc.addEventListener(MouseEvent.CLICK, gameover)
}
function gameover(e:MouseEvent):void
{
for each (var circle:addcircle in circles)
{
removeChild(circle)
}
circles.length = 0
** endGame()
}
}
public function movecirc(Eventclass:eventclass):void
{
var addcirc:addcircle;
addcirc = new addcircle((Math.random() * 1000000))
circles.push(addcirc);
addcirc.addEventListener(MouseEvent.CLICK, looper)
addChild(addcirc);
for each (var circle:addcircle in circles)
{
circle.x = (Math.random() * 600)
circle.y = (Math.random() * 400)
}
function looper(e:MouseEvent):void
{
addcirc.removeEventListener(MouseEvent.CLICK, looper)
dispatchEvent ( new eventclass( eventclass.LOOP))
addcirc.addEventListener(MouseEvent.CLICK, gameover)
}
function gameover(e:MouseEvent):void
{
for each (var circle:addcircle in circles)
{
removeChild(circle)
}
circles.length = 0
}
}
}
}
package Fun
{
import flash.display.Sprite;
public class endGame extends Sprite
{
public function endGame():void
{
}
}
}
如果没有调用endGame(),代码就会运行完美,并且我想要的一切都会发生。我只是不确定为什么endGame函数声称需要参数。
答案 0 :(得分:1)
我们在这里有几个问题。
首先:你应该总是使用分号,而不是你记得的时候。 第二:问题的原因是你正在调用endGame(),好像它是一个函数
你有一个类endGame,所以这是正确使用它:
var eg : endGame = new endGame();
从我看到你应该在使用Classes之前学到更多关于OOP的知识