Page 2 of 3

Re: Sound Effect / Forward + Jump?

Posted: Sat Dec 11, 2010 4:00 pm
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?

Re: Sound Effect / Forward + Jump?

Posted: Sat Dec 11, 2010 5:18 pm
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

Re: Sound Effect / Forward + Jump?

Posted: Sat Dec 11, 2010 5:57 pm
by Ryne
Ahh, I see. Thank's a lot! :)

Re: Sound Effect / Forward + Jump? / Collision

Posted: Sat Dec 11, 2010 7:27 pm
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

Re: Sound Effect / Forward + Jump? / Collision

Posted: Sat Dec 11, 2010 8:19 pm
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?

Re: Sound Effect / Forward + Jump? / Collision

Posted: Sat Dec 11, 2010 8:24 pm
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?

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

Posted: Sat Dec 11, 2010 8:28 pm
by Robin
Oh, well than that is the right approach.

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

Posted: Sat Dec 11, 2010 8:32 pm
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.

Re: Sound Effect / Forward + Jump? / Collision

Posted: Sat Dec 11, 2010 8:35 pm
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.

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

Posted: Sun Dec 12, 2010 1:46 pm
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.