I am new to Codea and Lua but thanks to some great tutorials by @Ignatz I am well on my way to creating a Tower Defense game.
Where I am running into problems is using the Tween function and maybe I am just using it wrong. What I am trying to do is tween a sprite from the tower to the enemy but the sprite just sits over the tower. If I try to tween a sprite in a separate project it works just fine. Am I trying to do something that the original function was not intended to be used for?
This is the code from the tower class which handles the shooting.
----------------------------------------------
-- Draws Defense Tower and Calculates Shots --
----------------------------------------------
Tower = class()
function Tower:init(x, y)
-- you can accept and set parameters here
self.x = x -- Tower X location
self.y = y -- Tower Y location
self.shot = false -- Whether tower Shot
self.shottimer = math.random(1,5) -- Set time delay on Tower Shot
end
function Tower:Shoot(locx, locy, Radius, EnemyHit)
if locx > self.x - Radius and locx < self.x + Radius and locy > self.y - Radius and locy < self.y + Radius and -- Test Enemy in Range
ElapsedTime >= self.shottimer and EnemyHit.Health > 0 then -- Test Shot Timer and Enemy Health
self.origin = {x = self.x, y = self.y} -- Tower Location
self.target = {x = locx, y = locy} -- Enemy location
-- Tween.Path bullet test did not work
-- tween.path(4, self.origin, {self.origin, self.target})
-- sprite("Space Art:Green Bullet", self.origin.x, self.origin.y, 10, 10)
-- Tower Laser
strokeWidth(5)
stroke(234, 25, 53, 255)
line(self.origin.x, self.origin.y, self.target.x, self.target.y)
self.shottimer = self.shottimer + math.random(1,5) -- Delay before next shot
EnemyHit.Health = EnemyHit.Health - 20 -- Add damage to Enemy
end
end
function Tower:draw()
-- Codea does not automatically call this method
sprite("Small World:Watch Tower", self.x, self.y, 50, 50) -- Current tower Sprite
-- Extra Towers
-- sprite("Small World:Church", 19 * 50 - 13, 35, 50, 50)
-- sprite("Small World:Fountain", 18 * 50 - 13, 35, 50, 50)
-- sprite("Small World:Observatory", 17 * 50 - 13, 35, 50, 50)
end
function Tower:touched(touch)
-- Codea does not automatically call this method
return false
end
@romspy Are you trying to do something like this. Tap the screen to start the tween.
function setup()
end
function draw()
background(40, 40, 50)
sprite("Space Art:Red Ship",100,300)
sprite("Small World:Tower",300,700)
if obj~=nil then
sprite("Space Art:Green Explosion",obj.x,obj.y)
end
end
function touched(t)
if t.state==BEGAN and obj==nil then
obj={x=300,y=770}
tween(1,obj,{x=100,y=300},tween.easing.linear,function() obj=nil end)
end
end
@romspy I just had it in the touched function to be able to control when it started. If that’s what you can use, then you’ll have to put the code where you need it since I didn’t see your code when I did this.