Newby..Can't rotate sprite twice! (SOLVED)

Hello there. Last year, I managed after many sweat to create a small drag and drop game in java. I can send you the download link.

In my game I’m moving in 2D small falt lego pieces on a lego board, trying to find a place to stay against other already placed piece

I tried many game engine and android studio and the rest of them, but I’m unable to make it a android game.

Trying Defold so far, I managed to create the main lego board, and the small lego piece. I figured out how to move it with the mouse, and even to rotate it on click. But rotation only work ONCE!

How can I make it rotate on will ?

Tks for your help.

Daniel

1 Like

Welcome to the Defold community!

In order to figure out what causes your problem you need to give more information. How does your code look like?

Hi Eisher. I based my code on the drag and drop example. Input is on mouse, when it collide with sprite, it can drag and drop. What I would like to do is double-clik, to be able to use it on android.
So, I had up a line, just simple click for now, sprite does rotate and then can be dragged again, but won’t rotate again.
I tried to do it with right-click, no rotation, but works with wheel-up!! (but always only once).

I tried to car tutorial, I managed to “drive the car” and there is rotation of wheel, but it is keyboard driven, input is on car. With drad and drop example, input is on cursor.
I will try animation, and have rotation inside frame, as I need a third state, when the sprite becomes unmovable (the lego piece will be stuck to the board)

Here is the code of drag and drop, that I adapted so far: (did it in defold 1 and Defold 2, same problem)

function init(self)
	msg.post(".", "acquire_input_focus")	
	self.collision_id = nil	
end

function final(self)
	msg.post(".", "release_input_focus")
end

function update(self)
	self.collision_id = nil
end

function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		if not self.collision_id or go.get_position(self.collision_id).z < message.other_position.z then
			self.collision_id = message.other_id
		end	
	end
end

function on_input(self, action_id, action)
	-- update cursor position
	go.set_position(vmath.vector3(action.x, action.y, 0))
	
	if self.collision_id and action.pressed then
		self.dragged_ts = socket.gettime()
		self.dragged_id = self.collision_id
	
		elseif action.released then	
		self.dragged_id = nil		
end

	
	-- we're dragging if we have a dragged_id and the time since action.pressed is larger than 0.5 seconds
	local is_dragging = self.dragged_id and (socket.gettime() - self.dragged_ts >= 0.5) or false
	
	-- if we're colliding with something and the user clicked we track the id and the time when it happened
	if self.collision_id and action.pressed then
		self.dragged_ts = socket.gettime()
		self.dragged_id = self.collision_id
		
		elseif action.released then
	
		self.dragged_id = nil		
	end

	if not action_id or action_id == hash("touch") and is_dragging then
		
		local url = msg.url(nil, "picot", "sprite")
		local widthpicot = go.get(url, "size.x")
   local heightpicot = go.get(url, "size.y")
     
--snap to grid
		go.set_position(vmath.vector3(math.floor((action.x)/16)*16+widthpicot/2, math.floor((action.y)/16)*16+1+heightpicot/2, 0), self.dragged_id)
							
			
		end
--rotation of sprite..I tried many things, this is done with wheel-up, I would like double click instead. I had to desactivate multi-touch for mouse to work. 

		if action_id == hash("rotate") then
--I tried self. ; self.collision_id ; self.dragged_id, etc..it always work only once. collision shape is rectangle the same size as sprite
		
local url = msg.url(nil, "picot", nil)	
			
	local rot = vmath.quat_rotation_z(math.pi / 2)
			
		go.set_rotation(rot ,url)

       end	
		
end
		
function on_reload(self)
    -- Add reload-handling code here
    -- Remove this function if not needed
    
end

You set the rotation to pi / 2, which is relative to the world (the go is not childed). Second time you set it it will point in the same direction.

What you probably want to do is to get the current rotation, calculate relative rotation and then combine them:

local current_rot = go.get_rotation()
local rot = vmath.quat_rotation_z(math.pi / 2)
go.set_rotation(current_rot * rot)
1 Like

Nice and fast anwser, thanks!

it worked with

go.set_rotation(current_rot*rot, self.dragged_id)

To ease the movement, I set it to both wheel up and wheel down, so it is nice , tks.

Why doesn’t it work with right-click ?

Or best, how to make it work with double-clik ? In the optic of double-tap in android…

You should be able to add an input binding to MOUSE_BUTTON_RIGHT. Doesn’t that work for you?

There is no out of the box support for double click, but you can add some logic to detect two single clicks within a certain timeframe. I have a gesture detector module that you can use to detect double click, long press, swipe etc.

I open a new subject, since the rotation issue was solved.

Nice to have this gesture detector! I will try it.