Sprint Button

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Volaik
Prole
Posts: 2
Joined: Mon Jan 20, 2014 6:24 pm

Sprint Button

Post by Volaik »

Hello, this is my first post on the LOVE forums and i'm semi-new to LUA. This game was just to see what i could do with LUA and LOVE

What I want to do is make the character move faster and when the button is released he goes back to his normal speed. I got him to move fast but when i release the button he does not go back to his original speed. The code will be below.

Code: Select all

Player = {}

function Player:new()
   o = {
       x = 30,
	   y = 30,
	   w = 20,
	   h = 20,
	   speed = 100,
	   score = 0,
    }
	setmetatable(o, {__index = player})
	return o
end	

function Player:update(dt)
   if love.keyboard.isDown("lshift") then -- this is the sprint line
      o.speed = 300
   end
   if love.keyboard.isDown("a") or love.keyboard.isDown("right") then
      o.x = o.x + o.speed * dt
   end
   if love.keyboard.isDown("d") or love.keyboard.isDown("left") then
      o.x = o.x - o.speed * dt
   end
   if love.keyboard.isDown("w") or love.keyboard.isDown("up") then
      o.y = o.y - o.speed * dt
   end
   if love.keyboard.isDown("s") or love.keyboard.isDown("down") then
      o.y = o.y + o.speed * dt
   end
   if o.x > love.graphics.getWidth() - o.w then
       o.x = love.graphics.getWidth() - o.w
   end
   if o.y > love.graphics.getHeight() - o.h then
       o.y = love.graphics.getHeight() - o.h
   end
   if o.x < 0 then
       o.x = 0
   end
   if o.y < 0 then
       o.y = 0
   end
end
	  
function Player:draw()
   love.graphics.setColor(255, 0, 0)
   love.graphics.rectangle("fill", o.x, o.y, o.w, o.h)
end
Thanks in advance.
I will post a love file if needed
Attachments
TestGame.love
Left shift is to sprint
(4.64 MiB) Downloaded 121 times
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Sprint Button

Post by Ranguna259 »

The speed isn't returning to normal because you forgot to actualy code that part:

Code: Select all

if love.keyboard.isDown("lshift") then -- this is the sprint line
  o.speed = 300
else
  o.speed = 100 --resets speed to normal
end
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Volaik
Prole
Posts: 2
Joined: Mon Jan 20, 2014 6:24 pm

Re: Sprint Button

Post by Volaik »

Thanks it worked
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Sprint Button

Post by Ranguna259 »

Welcome to the forum btw ;)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests