Combo Key Input

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
User avatar
alastaireSinclaire
Prole
Posts: 1
Joined: Tue Apr 16, 2019 10:28 pm

Combo Key Input

Post by alastaireSinclaire »

Hello,

I'm creating a 2d platformer for a class project.

I am attempting to implement multiple keystrokes to enable certain animations.
What would be the best method to implement something like this?
i.e. When I press 'shift' and 'd' the character will sprint in that direction. If only 'd' is pressed, a walking animation is played in that direction.

Also, what would be the best way to implement an animation chain?
Say for instance, I spam the 'e' button for an attack and the animation changes depending on how frequently the 'e' button is pressed. So the first time the 'e' button is pressed, one version of an attack animation is played. Then the second time the 'e' button is pressed immediately afterward, a second version of an attack animation is played and so on and so forth.

Essentially, I'm looking for some functionality you would see in Castlevania and Hollow Knight.

Thanks for any help and direction you can provide.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Combo Key Input

Post by raidho36 »

First of all, consider using animation and input libraries. There are wealth of them posted in the forums, and some of them are in the wiki https://love2d.org/wiki/Category:Libraries

Phrased differently, your problem is that you want to keep track of what state your character is in. Since (presumably) you only can play one animation at a time, you only need one state variable for this. Every time animation needs to change, overwrite this variable. And then you'd use this variable to select and play appropriate animation. With most animation libraries, this information would be retained inside the library, so you'd only have to switch current animation in appropriate moments (when you press or release shift, when you double-tap attack button, when you stop pressing anything, etc).
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: Combo Key Input

Post by PGUp »

For the walk animation.

Code: Select all

function love.update()
	if love.keyboard.isDown("d") and love.keyboard.isDown("lshift") then
		sprintAnimation:play()
	end
	if love.keyboard.isDown("d") and not love.keyboard.isDown("lshift") then
		walkAnimation:play()
	end
end
For the attack animation.

Code: Select all

function love.load()
timer = 0
animation_type = 1
animations = {} -- your animations here
end

function love.update(dt)
	timer = timer + dt
	if lk.isDown("e") then --meaning: if the key is pressed and the time inbetween those click is less than a second, then change the animation type
		if timer < 1 then 
			animation_type = animation_type + 1
		else --meaning: if the user only press the key once or doesnt press any key and it's over a second, then reset the animation to the first animation
			timer = 0
			animation_type = 1
		end
	end
	
	if animation_type > #animations then
		animation_type = 1
		--do something if there is no more animation to be played, in this case, reset it to the first animation, can be whatever you want
	end
	
		animations[animation_type]:play() --play the animation

end
-
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests