[Solved] how to move (background) display.newGroup

I’ve struggled for about 4 hours or so, now I’m whacked…
Could anyone help, please?

I have two display.newGroup() objects:
-local scene = display.newGroup()
-local allSceneElements = display.newGroup()

First one holds scene elements on which the hero can walk (map, trees,…).
The second one holds ALL elements on screen. It’s for “shaking”/sliding/breathing the environment, to have some motion while nothing happens.

Now I want to be able to Drag the allSceneElements around, in terms to explore the whole Level. I’ve tried endless combinations and possibilities I’m aware of, but, no success. After cleaning all shit out again, I have the fallowing code so far…
[lua]-- grabbing and exploring Scene
local container = display.newGroup()
container:insert(allSceneElements)

local function SceneExplore( event )
if event.phase == “moved” then
container.x = event.x
container.y = event.y
end
end
Runtime:addEventListener( “touch”, SceneExplore )[/lua]
I’ve added a third display.newGroup() which acts as a container - otherwise I wasn’t able to interact with it at all.

How can I accomplish my task in a good manner?

PS: By the way, there is an image inside “scene” Group, which is actually my map. It’s 2470×1940px large. [import]uid: 73502 topic_id: 18104 reply_id: 318104[/import]

So I’ve noticed I have same problem as him here:
https://developer.anscamobile.com/forum/2011/03/03/help-moving-display-group

Since my Group is very large (because of the map inside), the scrolling appears to be too sped up! How to fix that?

My code so far:
[lua]-- grabbing and exploring Scene
local container = display.newGroup()
container:insert(allSceneElements)

local function SceneExplore( event )
if event.phase == “began” then
container.xReference = event.xStart
container.yReference = event.yStart
elseif event.phase == “moved” then
container.x = (container.x + (event.x - event.xStart))
container.y = (container.y + (event.y - event.yStart))
end
if event.phase == “ended” then
– code
end
end
Runtime:addEventListener( “touch”, SceneExplore )[/lua] [import]uid: 73502 topic_id: 18104 reply_id: 69240[/import]

Really you need to put up something plug and play for people to test, or upload a sample.

Your background image is also too large - depending on what devices you hope to run it on it needs to be below 2048px or 1024px.

Have you considered tiles? (Lime is good for that.)

Peach :slight_smile: [import]uid: 52491 topic_id: 18104 reply_id: 69280[/import]

Feel free to test, please :slight_smile:

http://www.filesavr.com/U2STPWIBTUUZXUL

Why is my image too large? I thought I can use any size, since my Level can be whatever I like and I scroll it?!
The thing is, it has to be one image… I try to simulate one “superbrothers sword & sworcery ep” level. And I think they use only one large image. Correct me, if I’m wrong, please. [import]uid: 73502 topic_id: 18104 reply_id: 69337[/import]

you can scroll like,

[lua]local function startDrag( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

end
end

return true
end[/lua]

and yes you are wrong tell me if you can open this image in any mobile or table

http://wallshq.com/wp-content/uploads/original/2011_06/19_764-green-abstract-background_WallsHQ.com_.jpg

honeycomb and icecream may be exception
all you need to do is break your image in some parts put them side by side in one group and scroll that group
:slight_smile: [import]uid: 12482 topic_id: 18104 reply_id: 69341[/import]

[lua]local function SceneExplore( event )
local target = event.target
local phase = event.phase

if phase == “began” then

display.getCurrentStage():setFocus( target )
target.isFocus = true
target.x0 = event.x - target.x
target.y0 = event.y - target.y

end
if target.isFocus then

if phase == “moved” then
target.x = event.x - target.x0
target.y = event.y - target.y0

elseif phase == “ended” or phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
target.isFocus = false
end

end

return true
end
scene_container:addEventListener( “touch”, SceneExplore )[/lua]

Thanks. But there is one problem with your code suggestion: I want to move ALL my objects on screen, which are all in a [bash]local scene_container = display.newGroup()[/bash]. So I either have to set an Runtime event OR assign the listener to an Group object.
But this won’t work. Event.target does only work with images. I want however to set the focus on a Group as in my above example, or as a Runtime listener somehow…

Any further suggestions?

Btw, I tried to include your large image, and it showed up in the simulator, so why shouldn’t it work? I obviously can bring such large images into the engine?! [import]uid: 73502 topic_id: 18104 reply_id: 69344[/import]

try this

[lua]local function callMe(e)
if “began” == e.phase then
e.target.xPos = e.x - e.target.x
elseif “moved” == e.phase then
e.target.x = e.x - e.target.xPos
end

end
myGroup:addEventListener(“touch”,callMe)[/lua]

:slight_smile: [import]uid: 12482 topic_id: 18104 reply_id: 69345[/import]

i said for that image in actual device simulator is just for view it has not any limitation like mobile or tablet has [import]uid: 12482 topic_id: 18104 reply_id: 69346[/import]

[lua]-- grabbing and exploring Scene
local view = display.newGroup()
view:insert( scene_container )

local function SceneExplore( event )
local target = event.target

if event.phase == “began” then
target.xPos = event.x - target.x
target.yPos = event.y - target.y
elseif event.phase == “moved” then
target.x = event.x - target.xPos
target.y = event.y - target.yPos
print(event.x,target.x)
end
end
view:addEventListener( “touch”, SceneExplore )[/lua]
Thank you so much for your help! It works like I wanted it!
It didn’t worked first, because the [bash]scene_container[/bash] had more than one child (table) inside. But I noticed, you can drag a group only, when there is only one table inside. So I created another group and passed [bash]scene_container[/bash] to it. Since then it suddenly worked! :slight_smile:


PS: Ahh, ok, I understand why I was able to load such big image inside simulator. But isn’t it the same regarding to memory usage: to have one big image OR having several small images, loaded at same time? I think the memory will be the same, so what’s the clue? [import]uid: 73502 topic_id: 18104 reply_id: 69347[/import]

actually no it is not same its really hard to understand google for it for more info here’s the short description

graphics hardware works natively with textures or images in power-of-2 dimensions

means image of 100x100 will load as 128x128
200x300 will load as 256x512

now imagine if your image size is 2470×1940 then it will load as 4096x2048 (more wastage as we go higher right?)

so all the mobile devices are typically locked for the images with higher size then 2048x2048 some are even locked at 1024x1024
and it is a limitation of the device

:slight_smile:

[import]uid: 12482 topic_id: 18104 reply_id: 69352[/import]

Thank you so much! You’ve opened my eyes and helped me a lot! [import]uid: 73502 topic_id: 18104 reply_id: 69358[/import]