@Ignatz totally. Thank you. But I do have another problem I have been led into. My character needs to be moving at different speeds depending on the distance he is traveling (because obviously, I can’t have the tween duration set to 5 the whole time, or else a 2 foot walk will take forever, when in reality that’s not true). So I am using this little math formula I learned in math this year (I’m a 9th Grader) which determines the resulting length between two points on a graph. But because the screen isn’t a graph, and it only has positive “points” or pixels, the formula is broken. It gives me random huge numbers, instead of the real distance. And sometimes it is a negative number. It’s not possible to have a negative distance to travel.
It is the point-slope formula (at least I think that’s what it’s called). It looks like:
y2 - y1 / x2 - x1
As you’ll see in the code, I have used the touch position, and the current position of the sprite to complete the formula.
Player Class:
Player = class()
function Player:init()
self.position = {x = 384, y = 512}
self.health = 100
self.damage = 5
self.gold = 200
end
function Player:draw()
sprite("Documents:Character",self.position["x"],self.position["y"],40,40)
end
function Player:touched(touch)
-- This is where we will manage the movement of the character, and also how the player will attack (if tapped an enemy).
timing = (self.position["y"] - touch.y / self.position["x"] - touch.x) / 2
print(timing)
if timing < 100 then
timing = 3.5
elseif timing < 200 then
timing = 2.5
elseif timing < 300 then
timing = 2
elseif timing < 400 then
timing = 1.5
elseif timing < 500 then
timing = 1
elseif timing > 500 then
timing = 0.5
end
print(timing)
tween(timing,self.position,{x = touch.x, y = touch.y})
end
Main:
-- Project Name: Game 1
-- Description: Run around, loot chests, and survive.
-- Started on December 31st, 2015
-- Developer: Sean Wilkerson
-- Use this function to perform your initial setup
function setup()
-- The following comment is the template for a randomly generated Sprite positon.
-- vec2(math.random(WIDTH/2-300,WIDTH/2+300),math.random(HEIGHT/2-400,HEIGHT/2+400))
chestvalues = {}
chestpositions = {}
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
print("All game files have been loaded in.")
print("Physics engine loaded. Force algorythms and bodies have been applied.")
print("All stored data has been loaded. If any problems occur with lost items in inventory, or even lost coins and stuff, please contact us.")
-- Choose a random amount of coins to appear on the screen.
Player:init()
coinamount = math.random(1,4)
-- Insert random position values for up to 5 coins to use and occupy.
chestpositions[1] = vec2(math.random(WIDTH/2-240,WIDTH/2+240),math.random(HEIGHT/2-400,HEIGHT/2+400))
chestpositions[2] = vec2(math.random(WIDTH/2-240,WIDTH/2+240),math.random(HEIGHT/2-400,HEIGHT/2+400))
chestpositions[3] = vec2(math.random(WIDTH/2-240,WIDTH/2+240),math.random(HEIGHT/2-400,HEIGHT/2+400))
chestpositions[4] = vec2(math.random(WIDTH/2-240,WIDTH/2+240),math.random(HEIGHT/2-400,HEIGHT/2+400))
chestvalues[1] = math.random(10,50)
chestvalues[2] = math.random(10,50)
chestvalues[3] = math.random(10,50)
chestvalues[4] = math.random(10,50)
-- And next up, we will randomly pick where the Sentinel Tower will appear on the screen, and how much it will heal us for.
healthAmountChoice = math.random(70,100)
sentinelPosition = vec2(math.random(WIDTH/2-300,WIDTH/2+300),math.random(HEIGHT/2-400,HEIGHT/2+400))
end
function draw()
background(113, 119, 119, 255)
for coin = 1, coinamount do
Chests:init(chestvalues[coin],chestpositions[coin])
Chests:draw()
--fill(255, 255, 255, 255)
--fontSize(16)
--font("Noteworthy-Bold")
--text(Chests().tier.." Chest",Chests().chestposition.x,Chests().chestposition.y-25)
end
Sentinel:init(healthAmountChoice,sentinelPosition)
Sentinel:draw()
Player:draw()
Inventory:draw()
end
function touched(touch)
if touch.state == BEGAN then
Player:touched(touch)
end
end