Sound Effect / Forward + Jump? / Collision (kinda)

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.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Sound Effect / Forward + Jump?

Post by Ryne »

Hello again, I'm stuck on forcing the player to move forward while jumping as long as both keys are pressed. Right now if you run forward and hit the jump button it wont jump, unless you stand still.

I can think of ways of doing this but do I really have to create separate controls for if BOTH keys are pressed?

Code: Select all


	-- player controls
	if love.keyboard.isDown('right') then
		curranim = anim.runright
		player.dir = "right"
		player.x = player.x + 100 * dt
	elseif love.keyboard.isDown('left') then
		curranim = anim.runleft
		player.dir = "left"
		player.x = player.x - 100 * dt
	elseif love.keyboard.isDown('x') and player.dir == "right" then -- jump
		starttimer = true
		love.audio.play(audiosrc.jump)
		curranim = anim.jumpright
		jumping = true
	elseif love.keyboard.isDown('x') and player.dir == "left" then
		starttimer = true
		love.audio.play(audiosrc.jump)
		curranim = anim.jumpleft
		jumping = true
	else
		curranim = idleanim
end


Might as well ask about how to let the player fall before he can jump again. Right now holding the jump button forces him to stay in the air until it's released. I have a basic timer to force the audio to stop, I could technically use it to set a flag for "inair" or "onground". Unless there is an easier way?
@rynesaur
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Sound Effect / Forward + Jump?

Post by thelinx »

Ryne wrote:Hello again, I'm stuck on forcing the player to move forward while jumping as long as both keys are pressed. Right now if you run forward and hit the jump button it wont jump, unless you stand still.

I can think of ways of doing this but do I really have to create separate controls for if BOTH keys are pressed?

Might as well ask about how to let the player fall before he can jump again. Right now holding the jump button forces him to stay in the air until it's released. I have a basic timer to force the audio to stop, I could technically use it to set a flag for "inair" or "onground". Unless there is an easier way?
Your problem is the if statement.

Code: Select all

	-- player controls
	if love.keyboard.isDown('right') then
		curranim = anim.runright
		player.dir = "right"
		player.x = player.x + 100 * dt
	elseif love.keyboard.isDown('left') then -- if the first "if" check failed, check this.
		curranim = anim.runleft
		player.dir = "left"
		player.x = player.x - 100 * dt
	elseif love.keyboard.isDown('x') and player.dir == "right" then -- if the "if" checks before this failed, check this.
		starttimer = true
		love.audio.play(audiosrc.jump)
		curranim = anim.jumpright
		jumping = true
	elseif love.keyboard.isDown('x') and player.dir == "left" then -- if the "if" checks before this failed, check this.
		starttimer = true
		love.audio.play(audiosrc.jump)
		curranim = anim.jumpleft
		jumping = true
	else -- catch-all
		curranim = idleanim
end
You should change it into this:

Code: Select all

	-- player controls
	if love.keyboard.isDown('right') then
		curranim = anim.runright
		player.dir = "right"
		player.x = player.x + 100 * dt
	elseif love.keyboard.isDown('left') then
		curranim = anim.runleft
		player.dir = "left"
		player.x = player.x - 100 * dt
	end -- end the first if sequence and start a new one.
	if love.keyboard.isDown('x') then -- let's jump!
		starttimer = true
		love.audio.play(audiosrc.jump)
		jumping = true
		if player.dir == "right" then
			curranim = anim.jumpright
		elseif player.dir == "left" then
			curranim = anim.jumpleft
		end
		--[[ instead of that if sequence we could also do something sexy like this:
		curranim = anim["jump"..player.dir]
		--]]
	end
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Sound Effect / Forward + Jump?

Post by Ryne »

Ahh, I see. Thank's a lot! :)
@rynesaur
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Sound Effect / Forward + Jump? / Collision

Post by Ryne »

-- err, sorry for the double post.

Hi again, Right now my sprite images are 32x32. Though the sprite itself is less than that. I wan't to offset the hit-box (or bounding box) a little bit. Would it be practical to just give the hit-box itself it's own hitx and hity, or is there an easier way of pulling that off? Right now I'm using this code.

Code: Select all

function CheckCollision(obj1, obj2)

	if obj1.x > obj2.x + obj2.w - 1 or
	obj1.y > obj2.y + obj2.h - 1 or
	obj2.x > obj1.x + obj1.w - 1 or
	obj2.y > obj1.y + obj1.h - 1
	then
	
        return false             -- No collision. Yay!
    	else
        return true      -- Yes collision. Ouch!
		
    end
	
end
@rynesaur
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Sound Effect / Forward + Jump? / Collision

Post by Robin »

Ryne wrote:Hi again, Right now my sprite images are 32x32. Though the sprite itself is less than that. I wan't to offset the hit-box (or bounding box) a little bit. Would it be practical to just give the hit-box itself it's own hitx and hity, or is there an easier way of pulling that off?
It depends. What do you mean by offset the hitbox? Are all the sprites of different actual sizes or are they the same?
Help us help you: attach a .love.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Sound Effect / Forward + Jump? / Collision

Post by Ryne »

Robin wrote:
Ryne wrote:Hi again, Right now my sprite images are 32x32. Though the sprite itself is less than that. I wan't to offset the hit-box (or bounding box) a little bit. Would it be practical to just give the hit-box itself it's own hitx and hity, or is there an easier way of pulling that off?
It depends. What do you mean by offset the hitbox? Are all the sprites of different actual sizes or are they the same?
The sprites are IMAGES are 32x32, but the little sprite/character is actually smaller than that, though if I change the w/h of the hitbox it still goes by the IMAGE x/y. So the hitbox is drawn from the top left corner of the image. You know?
@rynesaur
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Sound Effect / Forward + Jump? / Collision (kinda)

Post by Robin »

Oh, well than that is the right approach.
Help us help you: attach a .love.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Sound Effect / Forward + Jump? / Collision (kinda)

Post by Ryne »

Robin wrote:Oh, well than that is the right approach.
lol... Consider the image itself like "canvas size", its 32x32. The character that's being drawn on that canvas is only like 20x25 or something. You know what.. I'm just going to upload an image. Consider the "RED" the hitbox.

Hitbox that's the full size of the image(32x32): Image

Smaller hitbox: Image

See how the hitbox is then drawn to the top left, I want it around the character.
@rynesaur
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Sound Effect / Forward + Jump? / Collision

Post by tentus »

Ryne wrote:
Robin wrote:
Ryne wrote:Hi again, Right now my sprite images are 32x32. Though the sprite itself is less than that. I wan't to offset the hit-box (or bounding box) a little bit. Would it be practical to just give the hit-box itself it's own hitx and hity, or is there an easier way of pulling that off?
It depends. What do you mean by offset the hitbox? Are all the sprites of different actual sizes or are they the same?
The sprites are IMAGES are 32x32, but the little sprite/character is actually smaller than that, though if I change the w/h of the hitbox it still goes by the IMAGE x/y. So the hitbox is drawn from the top left corner of the image. You know?
love.graphics.draw has offset x and offset Y parameters, I would use those for clarity.

Code: Select all

local offsetX = 6  -- 6px on each side
local offsetX = 7  -- 7px above
love.graphics.draw(self.image, self.x, self.y, 0, 1, 1, offsetX, offsetY)
Just a note: slightly off hitboxes end up being an enormous pain in my experience. I regret using them in my game, and you might end up in the same boat.
Kurosuke needs beta testers
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Sound Effect / Forward + Jump? / Collision (kinda)

Post by bartbes »

Regarding the sound: It hurts when I see this, please stop reloading sounds using newSource with static as 2nd arg, if you want multiple static sounds do it in a way that only requires 1 decoding operation, not everytime you play. The correct way for static sounds is creating SoundData, this contains the decoded sound, passing it to newSource will make it static, but not decode again.

Regarding hitboxes:
If you can, place the content of your images (before padding) top-left, that way you don't have to change position manually, and you just ignore the bit on the bottom and right.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests