所以,仍然在我的Tamagotchi风格游戏上工作,我正在使用单独的课程,根据这里的建议,如果可以的话,我会避免嵌套功能!
但是,我写了一些代码来根据hitTestObject
条件添加/删除食物,并且它们在主类中工作正常。
一旦我将所有内容移到Food Class
我就得到了上述错误。
我尝试过使用痕迹并评论出来......我很难过。
这有点令人沮丧,因为作为一个学习者,我只是觉得我开始破解这个并且 WHAM 我撞墙了,它让我想起我实际上一无所知。
无论如何,我会很感激一些建议。 这是我的两个类的代码。 再次感谢!
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import DraggableItem;
import Food;
public class Tamagotchi extends MovieClip {
const BG_SPEED:int = 10;
const BG_MINX:int = -270;
const BG_MAXX:int = 0;
const BG_MINY:int = -200;
const BG_MAXY:int = 0;
var bG:BackGround = new BackGround();
var home:Home = new Home();
var soph:SophRun = new SophRun();
var item:DraggableItem=new DraggableItem();
var food:Food = new Food();
public function Tamagotchi() {
addChildAt(bG, 0);
bG.addChild(home);
home.x = 850;
home.y = 80;
addChild(soph);
trace("HI");
soph.x = 350;
soph.y = 250;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressListener);
stage.addEventListener(Event.ENTER_FRAME, bgScroll);
}
public function keyPressListener(e:KeyboardEvent):void
{
if (e.keyCode == 65)
{
soph.x -= 5;
soph.scaleX = 1;
}
if (e.keyCode == 68)
{
soph.x += 5;
soph.scaleX = -1;
}
if (e.keyCode == 87)
{
soph.y -= 5;
}
if (e.keyCode == 83)
{
soph.y += 5;
}
}//End of KeyPressListener
public function bgScroll(e:Event)
{
if (soph.x > 550 && bG.x > BG_MINX)
{
bG.x -= BG_SPEED;
soph.x -=2;
}
else if (soph.x < 150 && bG.x < BG_MAXX)
{
bG.x += BG_SPEED;
soph.x +=2;
}
else if (soph.y > 400 && bG.y > BG_MINY)
{
bG.y -= BG_SPEED;
soph.y -=2;
}
else if (soph.y < 400 && bG.y < BG_MAXY)
{
bG.y += BG_SPEED;
soph.y +=2;
}
}//End of bgScroll
} //End of Class
} //包裹结束
这是Food Class
...
package {
import flash.display.MovieClip;
import flash.events.Event;
import Tamagotchi;
public class Food extends MovieClip{
var food1:Food1 = new Food1();
var food2:Food2 = new Food2();
var food3:Food1 = new Food1();
var food4:Food2 = new Food2();
var foodArray:Array = new Array(food1, food2, food3, food4);
var foodButton:FoodButton = new FoodButton();
var tam:Tamagotchi = new Tamagotchi();
public function Food() {
addChild(foodButton);
foodButton.x = 150;
foodButton.y = 650;
stage.addEventListener(Event.ENTER_FRAME, addingFood);
stage.addEventListener(Event.ENTER_FRAME, eatFood);
}
public function addButton(xpos:int, ypos:int)
{
addChild(foodButton);
foodButton.x = xpos;
foodButton.y = ypos;
}
public function addTheFood()
{
for (var i:int = 0; i < 4; i++)
{
addChild(foodArray[i]);
foodArray[i].x = 900 * Math.random();
foodArray[i].y = 650 * Math.random();
}
}
public function addingFood(e:Event)
{
if (tam.soph.hitTestObject(foodButton))
{
addTheFood();
removeChild(foodButton);
addButton(900 * Math.random(), 650 * Math.random());
}
}
public function eatFood(e:Event)
{
for (var j:int = 0; j < 4; j++)
{
if (tam.soph.hitTestObject(foodArray[j]))
{
removeChild(foodArray[j]);
foodArray[j].x=100000;
trace("Food gone!");
}
}
}
}//End of Class
} //包裹结束
很抱歉继续纠缠你们,但我决心要掌握这一点,你们似乎真的很了解你们的东西,所以我只是想学习! 要温柔!
答案 0 :(得分:1)
在您的Tamagotchi课程中
var food:Food = new Food();
在你的食物课上你有
var tam:Tamagotchi = new Tamagotchi();
这似乎是无限的循环...... (一个创建,创建另一个,再创建第一个,再创建另一个,依此类推......)