What sort of thing do you mean by “designate a set background”? [import]uid: 147322 topic_id: 35475 reply_id: 143495[/import]
I want to have a layer (background) that is a fixed position. no matter the boundaries of the camera view. [import]uid: 72845 topic_id: 35475 reply_id: 143501[/import]
For that, you can make layer 8’s parallax ratio 0 and add all of your background to it.
[lua]
camera:layer(8).parallaxRatio=0
[/lua]
Caleb [import]uid: 147322 topic_id: 35475 reply_id: 143505[/import]
Awesome! After messing around with it I’ve got a few more questions. Is it possible to look around with your finger by swiping the screen and also be able to follow a character once you press a button? And could you explain a little more about setting boundaries? For instance, how to set the boundary to be the size of the screen, or 2 times the size of the screen.
Thanks again, you’re restoring my faith in this project : ) [import]uid: 72845 topic_id: 35475 reply_id: 143638[/import]
Glad you’re liking it
Look Around With Finger -
My best idea for this is to make an invisible focus object that’s clamped inside of the camera’s bounds and when you swipe, move the invisible object accordingly.
Focus Object on Button Touch -
Simple Just use the :setFocus() inside of your button listener:
[lua]
–Using widget
local cameraBtn=widget.newButton{
default=“button.png”,
over=“button-over.png”,
onRelease=function() camera:setFocus(guy) end
}
[/lua]
Or something to that effect.
Camera Bounds Explanation -
Bounds are values that clamp the camera’s tracking range to a box. You set them with the setBounds function, which has four arguments - x1, y1, x2, and y2:
[lua]
camera:setBounds(x1, y1, x2, y2)
[/lua]
The x1 argument is the left side of the camera’s bounding box; y1 is the top of the camera’s bounding box; x2 is the right side of the camera’s bounding box; y2 is the bottom of the camera’s bounding box. By setting bounds, you’re telling the camera not to track the focus if it goes outside the bounds. But you must consider one thing -
Please note that the bounds go according to the X and Y of the object - not the sides of the camera view. So if you have an object at the very far left of the bounds, and the camera has no offsetX, the view will show half the screen size to the left of the object. You can see what I mean with this image: https://www.dropbox.com/s/1661np6bxy8n9t4/diagram.png
So there’s an explanation of bounds.
And, of course, simply use “false” (without quotes) as the first argument to stop the camera from having bounds at all.
Let me know how you get on
Caleb [import]uid: 147322 topic_id: 35475 reply_id: 143709[/import]
Thanks! I think I’m getting the hang of it. I noticed one thing though when I tried to add my other code to this new code. My problem is I have a button that creates (in this case a spring) object, and when I move my character the spring object moves with my character instead of staying on the screen where I positioned it. Is it possible to make the newly created object stay where I position it and still have my character interact with it. IE Click button > position spring > character jumps on to spring and bounces?
Here’s my existing spring code
[code]
–new spring code
_W = display.contentWidth
_H = display.contentHeight
–Our variable that contains number of springs
local springCount = 0
–Our button,that needs to be touched
local button = display.newImage( “springicon.png” )
button.x = display.contentWidth / 1.04
button.y = display.contentHeight / 12
– bounce function
local function bounce(obj,minScale,maxScale,time)
local oldXScale,oldYScale = obj.xScale, obj.yScale
local minScale = minScale or 0.8
local maxScale = maxScale or 1.2
local time = time or 100
if not obj.isBouncing then
obj.isBouncing = true
transition.to(obj,{time=time,xScale=maxScale,yScale=minScale,transition=easing.inQuad,onComplete=function()
transition.to(obj,{time=time,xScale=minScale,yScale=maxScale,transition=easing.outQuad,onComplete=function()
transition.to(obj,{time=time,xScale=oldXScale,yScale=oldYScale,onComplete=function() obj.isBouncing = false end})
end})
end})
end
end
–Function to move spring, use later in another function
local function move_spring(event)
event.target.x = event.x
event.target.y = event.y
if event.phase == “began” or event.phase == “ended” or event.phase == “cancelled” then
bounce(event.target)
end
end
– collision listener function
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
bounce(self) --if this doesnt work, bounce(event.target) or bounce(spring)
–spring sound FX
springSound = audio.loadStream(“spring.mp3”)
backgroundMusicChannel = audio.play( springSound, { channel=2, loops=0 } )
–spring sound FX END
end
end
–Function to create string at needed x and y; add 1 to springCount per created string
–and add event listener to created string
– colision function
function create_string()
if springCount < 1 then --how many springs you can add
local spring = display.newImageRect( “Spring.png”,50,50)
physics.addBody( spring, “static”, { density=1, friction=0, bounce=1 } )
spring.x = 400
spring.y = 130
springCount = springCount + 1
spring:addEventListener(“touch”, move_spring)
– Adds collision listener to spring
spring.collision = onLocalCollision
spring:addEventListener( “collision”, spring)
elseif springCount == 1 then–how many springs you can add
print(“stop”) – there you can do anything you want, i just used print for debugging purposes
end
end
–With this function we touch the button and it changes its alpha and use create_string()
local function touch_button(event)
if event.phase == “began” then
print(“began”)
button.alpha = 0.5
elseif event.phase == “ended” then
print(“ended”)
button.alpha = 1
create_string()
end
end
– At last we adding event listener to button and watch magic happens:)
button:addEventListener(“touch”, touch_button)
—end new spring code
[/code] [import]uid: 72845 topic_id: 35475 reply_id: 143860[/import]
You need to add your spring to the camera:
[lua]
function create_string()
if springCount < 1 then --how many springs you can add
local spring = display.newImageRect( “Spring.png”,50,50)
physics.addBody( spring, “static”, { density=1, friction=0, bounce=1 } )
spring.x = 400
spring.y = 130
springCount = springCount + 1
spring:addEventListener(“touch”, move_spring)
– Adds collision listener to spring
spring.collision = onLocalCollision
spring:addEventListener( “collision”, spring)
camera:add(sprint, 1) – Add it to the same layer as the character
elseif springCount == 1 then–how many springs you can add
print(“stop”) – there you can do anything you want, i just used print for debugging purposes
end
end
[/lua]
Caleb [import]uid: 147322 topic_id: 35475 reply_id: 143887[/import]
Tried that. right now I have a character on a ledge, if I make the character walk off the ledge the spring I create falls with the character so they never collide. [import]uid: 72845 topic_id: 35475 reply_id: 143899[/import]
Are you getting any errors? Of any sort?
C [import]uid: 147322 topic_id: 35475 reply_id: 144003[/import]