2

I'm currently using giderous studio to make a tower defense game. However the code doesn't run as expected.

I made a function that make the monster in the game move according to the route settled in a JSON file. The move process go well EXCEPT the first monster being spawned firet, I have absolutely no ideas why the monster stop on its way. (As shown as diagram) enter image description here

The following is my code:

level.lua:

    level = Core.class(Sprite)

function level:init(levelId)
    local level = dataSaver.load("level");

    --load attributes
    self.lives=level[tostring(levelId)]["lives"];
    self.route=level[tostring(levelId)]["route"];
    self.round=level[tostring(levelId)]["rounds"];
    self.towersAllowed=level[tostring(levelId)]["towers"];

    --some varibles
    self.currentRound=1;
    self.monsterList={};
end

function level:spawnMonster()
    local monsterType=self.round[1]["type"];
    local spawnNumber=self.round[1]["number"];

    --set timer for spawn
    local timer = Timer.new(self.round[1]["spawnInterval"],0);--spawnNumber);
    timer:start();

    --spawn
    timer:addEventListener(Event.TIMER,function()
        local spawnedMonster = monster.new(monsterType);
        spawnedMonster.instance:setPosition(self.route[1]["x"],self.route[1]["y"]);
        self:moveMonster(spawnedMonster);
    end);

    --after spawn finished, wait for next round.
    timer:addEventListener(Event.TIMER_COMPLETE,function()
        local nextRound = Timer.new(self.round[1]["timeUntilNextRound"],1);
        nextRound:start();
        nextRound:addEventListener(Event.TIMER_COMPLETE,function()
            print("nextRound");
        end);
    end);
end

function level:moveMonster(monsterToMove)
    if  (monsterToMove.currentDes<=#self.route) then
        monsterToMove:move(self.route[monsterToMove.currentDes]["x"],self.route[monsterToMove.currentDes]["y"]);
        monsterToMove:addEventListener("monsterMoveDone",function()
            monsterToMove.currentDes=monsterToMove.currentDes+1;
            self:moveMonster(monsterToMove);
        end);
    end
end

monster.lua:

monster = Core.class(Sprite)

function monster:init(type)
    local monstersList = dataSaver.load("monster");
    self.instance = quickImage(monstersList[type]["image"], _W/2, _H/2,monstersList[type]["width"], monstersList[type]["height"]);
    self.speed=monstersList[type]["speed"];
    self.health=monstersList[type]["health"];
    stage:addChild(self.instance);

    ---some varibles...
    self.currentDes=2
end

function monster:move(desX,desY)
    local time=getDistance(math.abs(self.instance:getX()-desX),math.abs(self.instance:getY()-desY))/self.speed
    local mc = MovieClip.new{
        {1, time, self.instance, {x = {self.instance:getX(), desX, "linear"}}},
        {1, time, self.instance, {y = {self.instance:getY(), desY, "linear"}}},
    }
    mc:addEventListener(Event.COMPLETE,function()
        self:dispatchEvent(Event.new("monsterMoveDone"))   
    end);
end

from the code, quickImage(); is just a little function for making image sprite Any ideas, I thought maybe i make the memory allocating wrong.

Special Note. I run Gideros Studio under OpenSUSE 12.2 with Wine, but i don't think its a matter because all stuff go well.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Peter Chan
  • 63
  • 1
  • 4

0 Answers0