Hello,
Today I am trying to create a jumping mechanic and I am a bit lost. I do not know exactly how I would do it. I have looked at many different posts and assets and I am still lost. Here is what I have…
local min_y = 30
local max_y = 680
local min_x = 30
local max_x = 1225
function init(self)
msg.post(".", "acquire_input_focus")
self.vel = vmath.vector3()
end
function update(self, dt)
local pos = go.get_position()
pos = pos + self.vel * dt
self.vel.x = 0
self.vel.y = -200
if pos.y < min_y then
pos.y = min_y
elseif pos.y > max_y then
pos.y = max_y
end
if pos.x < min_x then
pos.x = min_x
elseif pos.x > max_x then
pos.x = max_x
end
go.set_position(pos)
self.vel.x=0
end
function on_message(self, message_id, message, sender)
--Handle collision
if message_id == hash("contact_point_response") then
--local newpos = go.get_position() + message.normal * message.distance
--go.set_position(newpos)
self.vel.y=0
end
end
function on_input(self, action_id, action)
if action_id == hash("up") then
self.vel.y = 400
elseif action_id == hash("down") then
self.vel.y = -400
elseif action_id == hash("left") then
self.vel.x = -400
sprite.set_hflip("#sprite", false)
elseif action_id == hash("right") then
self.vel.x = 400
sprite.set_hflip("#sprite", true)
elseif action_id == hash("down") then
self.vel.y = -400
end
end
Now I don’t quite know how to implement a jump, and I have read over multiple posts and I still feel lost…
Eventually, I would like to be able to add double jumps and wall jumps, but I can’t even get a normal jump haha.
If anyone could take a look at my code and let me know what I can add and/or tweak with what I have it would be greatly appreciated.
Also, I have taken a look at platypus and it seems extremely convenient (and I would love to use it) but I’m not quite sure how it works. I have added the library dependancy and copied and pasted in the code that was listed under usage at this site. If anyone has insight on using platypus or creating my own code or directing me with platypus it would be very appreciated
(sorry for all the questions lately, I have just been a bit lost.)
If you wish to base your game on the code from the Platypus example project then you need to make sure to add a dependency to Platypus plus whatever other depends that there are in game.project (like defold-input).
At this point, I decided to look at the Grottoescape example within the Platypus example and found that the player script was referring to a few different things:
So I decided to add these to my player code, but it still returned the same errors…
I’m not quite sure what to do now. My goal is to just make platformer movement. I am very sorry for my little knowledge of the subject, and your assistance, guidance, and patience with me is very much appreciated.
I opened the project and ran it. The console showed the following:
INFO:DLIB: Log server started on port 61228
INFO:ENGINE: Engine service started on port 61229
INFO:ENGINE: Defold Engine 1.2.159 (f7d0a1b)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device 'default'
ERROR:SCRIPT: /platypus/platypus.lua:58: You must provide a config
stack traceback:
[C]: in function 'assert'
/platypus/platypus.lua:58: in function 'create'
/test level/character/platypus script.script:7: in function </test level/character/platypus script.script:6>
This is a pretty clear error message. The platypus library gives an error saying that “You must provide a config” and it even mentions in which file and line the error originated from:
/test level/character/platypus script.script:7: in function </test level/character/platypus script.script:6>
Line #6 in platypus script.script is this:
1. local platypus = require "platypus.platypus"
2. local input = require "in.state"
3. local input_mapper = require "in.mapper"
4.
5. function init(self)
6. self.platypus = platypus.create(config)
7. end
And the problem is that config is undefined. You must provide a configuration table. This is done in the example:
Thank you so much! That really helped! A few more things then I will be out of your hair:
The collisions seem a bit out of wack, when the gameobject lands on tiles it does not collide when it should, and created a product that looks like this:
The next thing, would it be possible to tweak some of the input for mobile gameplay? How might I go about doing that?
And finally, I believe to keep dependency changes permanent I would have to copy all of the scripts and files from the dependencies into the game itself? I remember having an issue with it once.
Thank you so much again for your time and help!
It is likely that the collision object shapes are wrong or that you haven’t configured Platypus properly (check the config table that you passed to Platypus!). Start by enabling physics debug so that you see the shapes:
There is also a debug setting on Platypus that you can enable.
What do you mean specifically?
If you make changes to files that belong to a dependency then you need to make a copy of the library. But you really shouldn’t change any files belonging to your dependencies.
Thanks very much for the debug help, I was able to figure out the problem!
When I was talking about mobile game play, I was wondering if, using the platypus dependency, would it be possible to create a mobile game? Would input (like arrow keys or wasd) somehow be able to be altered for use on a mobile device. I was thinking something like buttons on a mobile screen. Would it be possible to somehow use buttons on a mobile screen rather than arrow keys or wasd?
Yes, this is of course possible. Platypus doesn’t require input to come from a specific source. In fact Platypus doesn’t care at all. You call functions such as platypus.left(), platypus.right() and platypus.jump() to control the player. You can use keyboard, gamepad or on screen buttons to detect interaction and call the appropriate platypus functions.
Hey that’s great! So if I were to have an onscreen button and every time it was clicked it was to call platypus.left() or platypus.right() and send that message to the player.script, it would move the character? If that’s the case this is great! Thanks so much for all your help! Sorry if I have been driving you a bit nuts but you have really helped me!
Have a good one!
Yes, that’s how it would work. The on-screen buttons will exist in a gui component and when pressed they will send messages to your player script which in turn will call platypus functions.
I have recently returned to this topic as my Apple Developers Program’s info is pending and I have run into a bit of an issue…
I have managed to make both the left and the right buttons work. All I had to do was move them from the on_input function over to the on_message function and change action_id to message_id, but I could not figure out how to do the same thing with the platypus jump command since it uses action.pressed and action.released.
This is my script. It works just fine with the left and right functions but I cannot figure out how to make the jump function work. (These lines of code are inside the on_message function)
You’d have to send if the GUI button was pressed or released along with the message. Something like msg.post("/player", "jump", {pressed = true, released = false}). So you might put
function on_input(self, action_id, action)
if action_id == hash("touch") then
if gui.pick_node(jump_button, action.x, action.y) then
if action.pressed then
msg.post("/player", "jump", {pressed = true, released = false})
elseif action.released then
msg.post("/player", "jump", {pressed = false, released = true})
end
end
end
end
in your GUI script and replace action.pressed and action.released in the player script with message.pressed and message.released. Of course, you’d have to replace some of the above code to fit with your existing scripts.