Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Camera and Physics Problem
Started by test295 May 27 2019 08:50 AM

2 replies to this topic
physics camera
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

test295

[GLOBAL: userInfoPane.html]
test295
  • Observer

  • 5 posts
  • Corona SDK

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

display.setStatusBar( display.HiddenStatusBar )
system.activate( "multitouch" )

require "physics"
physics.start()

local width = display.actualContentWidth
local height = display.actualContentHeight

local myGroup = display.newGroup()

local ground = display.newRect( myGroup, width * 0.5, height - 5, width, 10 )
ground:setFillColor( 0.1 )
physics.addBody( ground, "static" )

local player = display.newRect( width * 0.5, height * 0.5, 100, 100 )
player:setFillColor( 0.93, 0.11, 0.14 )
physics.addBody( player )

local left = display.newRect( 100, height - 100, 200, 200 )
left.alpha = 0.1

local right = display.newRect( width - 100, height - 100, 200, 200 )
right.alpha = 0.1

local up = display.newRect( width - 300, height - 100, 200, 200 )
up.alpha = 0.1

local speed = 0

local function touchListener( event )
	if event.phase == "began" then if event.target == left then speed = -10 else speed = 10 end
	elseif event.phase == "ended" or event.phase == "cancelled" then
		speed = 0
	end
end

left:addEventListener( "touch", touchListener )
right:addEventListener( "touch", touchListener )

local function update()
	myGroup.x = myGroup.x - speed
end

Runtime:addEventListener( "enterFrame", update )
I wanted to simulate a camera so used a display group. Player doesn't move, it is the group that moves. It works but when my player go left and left and left when there is no ground, i mean when the player doesn't collide the ground, it should fall but it doesn't fall. What is wrong with my code? Thanks in advance. I writed topic so fast so sorry for my english.

[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,585 posts
  • Corona SDK

Have you verified your touch listeners?  Those look wrong to me.


This is how I implement touch listeners.

local speed = 0
local function touchListener( self, event )
   if( event.phase == "began" ) then 
      speed = speed + self.speed
   end
   elseif( event.phase == "ended" ) then 
      speed = speed - self.speed
   end
end

local left = display.newRect( 100, height - 100, 200, 200 )
left.alpha = 0.1
left.touch = touchListener
left.speed = -10

local right = display.newRect( width - 100, height - 100, 200, 200 )
right.alpha = 0.1
right.touch = touchListener
right.speed = 10

left:addEventListener( "touch" )
right:addEventListener( "touch" )

Edited by roaminggamer, 27 May 2019 - 09:28 AM.


[TOPIC: post.html]
#3

test295

[GLOBAL: userInfoPane.html]
test295
  • Observer

  • 5 posts
  • Corona SDK

I can't solve my problem :/ My player doesn't fall after walking out of platform.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: physics, camera