Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

best way to detect an object being shake while touched?
Started by Scott Harrison Mar 12 2016 03:46 PM

No replies to this topic
shake object
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,765 posts
  • Enterprise

what is the best way to detect if an object is being shaken while touched?

It is just there are a lot blocks in my level editor so I need to make my block tool bar smaller

I was wanting to do something like Mario maker (here is video if you have not seen)

Any idea on what the best way to do this is?

Thanks,

Scott

 

Edit: 

local myRect = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100 )
local howManyShakes = 5
local shakeCount = 0
local shakeTimer
local function shakeItUp( )
	print("I am dizzy" )
end
local function myRectTouch( event )
	local self = event.target
	if event.phase == "began" then
		self.dir = "left"
        self.markX = self.x    -- store x location of object
        self.markY = self.y    -- store y location of object
	
    elseif event.phase == "moved" then
		
        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY
        
        self.x, self.y = x, y    -- move object based on calculations above
        -- time to shake it up

        if (self.lastX and self.dir == "left" and event.x > self.lastX+10 ) then
        	self.dir = "right"
        	shakeCount = shakeCount+1
        elseif (self.lastX and self.dir == "right" and event.x < self.lastX-10 ) then
        	self.dir = "left"
        	shakeCount = shakeCount+1
        end
        local function shakeTimerHandle( )
        	shakeCount = 0
        	self.lastX = event.x
        end
        if (shakeTimer == nil) then
        	self.lastX = event.x
        	shakeTimer = timer.performWithDelay( 1500, shakeTimerHandle, 0 )
        end
        if (shakeCount >= howManyShakes) then
        	shakeCount = 0
        	shakeItUp()
        end
    elseif event.phase == "ended" or event.phase == "cancelled" then
    	if (shakeTimer) then
    		timer.cancel( shakeTimer )
    		shakeTimer = nil
    	end
    end
    
    return true
end

myRect:addEventListener( "touch", myRectTouch )



[topic_controls]
[/topic_controls]