Author Topic: 2 Player  (Read 11583 times)

0 Members and 1 Guest are viewing this topic.

yankscally
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: 2 Player
« Reply #15 on: May 02, 2017, 11:28:26 PM »
@Diarandor
Ah! I did not notice that tricky math variable.

@yankscally
No need for the math variable.

Your code worked, straight up. Thank you Zefk! A little bit of time writing that code for you would have taken me days to figure out, but I would have got there eventually. I'm from an music/art background so im no wizard but I do know the basics of code, I really appreciate the help! If you need some sounds for your game, I will happily supply them I'm working on some sound packs that are pretty neat, including a very nice sounding dialog menu, I could show you if you are interested!

Zefk

  • Hero Member
  • *****
  • Posts: 535
  • Just helping Solarus
    • View Profile
    • Zefk Design
Re: 2 Player
« Reply #16 on: May 02, 2017, 11:32:06 PM »
You are welcome. I updated that post about camera movements and other info. You will still have to set the direction as I mentioned.
http://forum.solarus-games.org/index.php/topic,962.msg5558.html#msg5558

Code: ( lua) [Select]
local entity = ...
local game = entity:get_game()
local map = entity:get_map()
local hero = map:get_hero()
local sprite
local straight = sol.movement.create("straight")
menu = false
local camera = map:get_camera()
camera:start_tracking(entity)


---Create sprite
function entity:on_created()
  sprite = entity:create_sprite("hero/tunic3"):set_animation("stopped")
  entity:set_can_traverse("hero", false)
  entity:get_direction(2)
  straight:set_speed(80)
end
 
--CONTROLS
--UP
function player_up()
 straight:set_angle(math.pi / 2)
 straight:start(entity)
end
 
--DOWN
function player_down()
 straight:set_angle(3 * math.pi / 2)
 straight:start(entity)
end

--Left
function player_left()
 straight:set_angle(math.pi)
 straight:start(entity)
end
 
--right
function player_right()
 straight:set_angle(0)
 straight:start(entity)
end


--Key press function
function sol.main:on_key_pressed(key)

--right
  if key == "l" and menu == false then
    player_right()
    entity:get_sprite():set_animation("walking")
  end

--left
  if key == "j" and menu == false then
    player_left()
    entity:get_sprite():set_animation("walking")
  end

--down
  if key == "k" and menu == false then
    player_down()
    entity:get_sprite():set_animation("walking")

  end

--up
  if key == "i" and menu == false then
    player_up()
    entity:get_sprite():set_animation("walking")
  end
end


function sol.main:on_key_released(key)

   if key == "l" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end
   
   if key == "j" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end
   
   if key == "k" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end
   
   if key == "i" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end

end

Zefk

  • Hero Member
  • *****
  • Posts: 535
  • Just helping Solarus
    • View Profile
    • Zefk Design
Re: 2 Player
« Reply #17 on: May 03, 2017, 08:20:47 AM »
I made a quick library example for the custom entity and attached it to this post. I made it mostly for the book project, but it can be used to shorten the script above. I included documentation and you will need a markdown reader like haroopad.

If you want to use it, then the script would look like this:



Code: ( lua) [Select]
local entity = ...
local game = entity:get_game()
local map = entity:get_map()
local hero = map:get_hero()
local sprite
menu = false


local camera = map:get_camera()
camera:start_tracking(entity)

---Create sprite
function entity:on_created()
  sprite = entity:create_sprite("hero/tunic1"):set_animation("stopped")
  entity:set_can_traverse("hero", false)
  entity:speed(80)
end

--Key press function
function sol.main:on_key_pressed(key)

--right
  if key == "l" and menu == false then
    entity:right()
  end

--up
  if key == "i" and menu == false then
    entity:up()
  end

--left
  if key == "j" and menu == false then
    entity:left()
  end

--down
  if key == "k" and menu == false then
    entity:down()
  end
end

function sol.main:on_key_released(key)
   --right
   if key == "l" then
       entity:stop()
   end

   --up
   if key == "i" then
       entity:stop()
   end

   --left
   if key == "j" then
       entity:stop()
   end

   --down
   if key == "k" then
       entity:stop()
   end
end

Diarandor

  • Hero Member
  • *****
  • Posts: 1062
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: 2 Player
« Reply #18 on: May 03, 2017, 08:51:34 AM »
It is really cool to see that working.  :)
The feature of switching heroes would be very useful too for your project.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”