Author Topic: <SOLVED> Switch between two sprites by pressing a key?  (Read 323 times)

Eyth

  • Newbie
  • *
  • Posts: 19
    • View Profile
<SOLVED> Switch between two sprites by pressing a key?
« on: May 17, 2018, 02:35:10 pm »
Hello there  ;)
New here and just started with Solarus and the programming-thing  :P
First, thank you for such an amazing programm! Great job.

My question:
I already have my own character-sprite implimented as the "hero". The thing is, that my idea is, to be able to switch the sprite of my hero between two sprites everytime I press a certain key. (F.e. I press "w" -> sprite of hero changes to "tunic1", I press "w" (again) -> sprite of hero changes to "tunic2", and back, and back...)
I already tried some things myself, but can't really figure it out...
(Still got problems with the whole "game:get things, game:set things" thing ^^)
« Last Edit: June 20, 2018, 11:16:32 am by Eyth »

Max

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Switch between two sprites by pressing a key?
« Reply #1 on: May 17, 2018, 05:59:00 pm »
One thing you could try is going into your main.lua script. There's some lines in there that handle key presses, I think the function is sol.main:on_key_pressed? Something like that. Anyway, that handles special keys, you can see there the format is stuff like, if key==f5 do this, if key ==escape, do this. You'll want some code like:

If key == "w" then
If game:get_ability("tunic") == 1 then game:set_ability("tunic", 2)

And vice versa. If you're still not familiar enough with Lua to notice, what I typed is not correct code (there are no "end"s for the conditional branches), so don't try copy/pasting or it'll just give you errors, haha, but that's the general idea which I think should work. You can look those methods up in the Solarus documentation and ask more questions of you don't understand them : )

Eyth

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Switch between two sprites by pressing a key?
« Reply #2 on: May 22, 2018, 11:36:56 am »
Thanks for the tip ;)
I understand the lua-concept a little bit. I know what you mean with the "ends" ;)
I'll try your advice, since that is something I've not tried yet ^^

Eyth

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Switch between two sprites by pressing a key?
« Reply #3 on: May 22, 2018, 12:46:45 pm »
So I tried Max's advice and had to play a little bit around. But IT WORKED!!
I post my result, if anyone has a similar task ;)
I tried so many things and as often with new programs, overcomplicated it  :P

I now didn't go with the "get_ability()" method in the main lua, but with "set_tunic_sprite_id()" directly in the hero.lua (see below)
I set up two sprites (playersprite1 & playersprite2) and now let the engine change between those two whenever "w" is pressed.
Exactly what I wanted, thanks again Max for the great tip, which made it work: ;)

Code: Lua
  1. --Initialize the behaviour of the hero in the quest--
  2.  
  3. require("scripts/multi_events")
  4.  
  5. local hero_meta = sol.main.get_metatable("hero")
  6.  
  7. local function initialize_player(game)
  8.                 local player = game:get_hero() --Took the name "player", because the sprite has the same name--
  9.                 player:set_tunic_sprite_id("hero/playersprite1") --Sprite to begin with--
  10.  
  11.       function sol.main:on_key_pressed(key, modifiers) --key-press function--
  12.  
  13.         local handled = false
  14.         if key == "w" then --change on key "w"--
  15.               if player:get_tunic_sprite_id() == "hero/playersprite1" then --if current sprite is sprite1, then--
  16.                  player:set_tunic_sprite_id("hero/playersprite2") --change to sprite2--
  17.               elseif player:get_tunic_sprite_id() == "hero/playersprite2" then --and back--
  18.                      player:set_tunic_sprite_id("hero/playersprite1") --to sprite1--
  19.               end
  20.           handled = true
  21.         end
  22.         return handled
  23.       end
  24. end
  25.  
  26. --Set up the hero features on any game that starts--
  27. local game_meta = sol.main.get_metatable("game")
  28. game_meta:register_event("on_started", initialize_player)
  29. return true
  30.  
« Last Edit: May 23, 2018, 07:10:22 pm by Neovyse »

Neovyse

  • Administrator
  • Full Member
  • *****
  • Posts: 118
    • View Profile
Re: Switch between two sprites by pressing a key?
« Reply #4 on: May 22, 2018, 01:31:37 pm »
Tip: you can write lua with automatic syntax coloring. Click on the "Insert code" button and write code=lua in the first token.


Code: Lua
  1. local function my_function()
  2.   -- content
  3. end

llamazing

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: Switch between two sprites by pressing a key?
« Reply #5 on: May 22, 2018, 02:52:47 pm »
      function sol.main:on_key_pressed(key, modifiers) --key-press function--
You don't want to use sol.main:on_key_pressed() here. You should only use this function for responding to key presses that are applicable at all times while your quest is running, including when there is not an active game (such as the character select or title screen). The way you have it now you'll probably get an error if the player exits to the title screen and presses the w key because game and hero won't exist any more. Use game:on_key_pressed() instead.

You could also run into problems if you have multiple scrips calling game:on_key_pressed() as the scripts will end up overriding each other. You might be able to get away with using multi_events for game:on_key_pressed() depending on what you are trying to do. A better solution might be to create a menu to handle processing the key presses and call menu:on_key_pressed() instead.

Diarandor

  • Hero Member
  • *****
  • Posts: 1010
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Switch between two sprites by pressing a key?
« Reply #6 on: May 22, 2018, 03:48:04 pm »
A very old video (my first video) may motivate you and shows things you can do.
https://www.youtube.com/watch?v=iY-yckilRrg&t=0s&list=PLysNP7i5PKBCgT898-xreWyPcaByEB6Rg&index=72
My switch hero system is different from yours (you want one like in Secret of Mana, which is simpler) and mine is like the one in Lost Vikings games.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Eyth

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Switch between two sprites by pressing a key?
« Reply #7 on: May 28, 2018, 10:11:23 am »
@neovyse: Thanks for the tip, I already thought, there was something like this ^^

@Ilamazing: Thanks for the reply. That makes sense. It wasn't a problem till now, because I haven't made a titlescreen for now. But I will consider it  ;)

@Dirandor: Thanks for the link. I will watch it. Maybe I can use something from it  :)

The code/script is constantly improving and evolving. For now its working perfectly. Thanks again