Items bar lock view

Here is my problem:

I have a very simple game, but at one point you can move these little tiles around. What I want to happen is for my tiles to have two positions possible:

  1. On the stage, wherever the touch ended, and
  2. In the items bar.
    So I set it to, if the y of the touch was under a certain amount, put it in the items bar. Now, my character moves and the master group follows it. What I want to happen is for if the tile is in the items bar, it to not move with the character, but if it is on the stage, to move with the character. How should I do that? I tried calling “itemsBox:insert(tile)” and then “game:remove(tile)” but it took the tile completely off the screen.

binc [import]uid: 147322 topic_id: 27068 reply_id: 327068[/import]

please help! [import]uid: 147322 topic_id: 27068 reply_id: 110519[/import]

Hi @binc.games

Just a couple things…

First, labeling your post “URGENT!!!” with no other details and when the content of the post really isn’t urgent, isn’t going to get you much help on here.

Secondly, I’m not sure I 100% understand your problem, and with out some additional context or code to reference it’s going to be really hard for anyone to help you.

Give us a little more to work on.
[import]uid: 19626 topic_id: 27068 reply_id: 110526[/import]

How long is a piece of string?
How deep is a hole?
How long is a river?
As Rob pointed out, give some more specifics onto exactly what you are trying to do.

Also, maybe some sample code would help, don’t you think?

:slight_smile:

ng [import]uid: 61600 topic_id: 27068 reply_id: 110528[/import]

Ok, sorry for naming it that, only when I just named it “remove from group object is hidden” nobody seemed to answer, and I can’t proceed without it being fixed, so it is urgent for me. I can rename it.

Here is more information:

  1. When my character moves, the view follows it
  2. You can move tiles that “power up” your character on collision
  3. There is a bar at the top of the screen that holds the tiles until you move them off
  4. If the touch is inside the bar, the tile goes into it with an alignment function I made
  5. If the touch is outside the bar, the tile goes where the touch is

What I want to happen is if the tile is in the bar, the tile needs to stay put and not move with the character, but if the tile is off the bar it needs to move with the character.

I’m sorry, but just now my text editor crashed and I have to start over from a while ago…like a LONG while ago.

binc [import]uid: 147322 topic_id: 27068 reply_id: 110529[/import]

Keep in mind that when you insert an object into another group, you usually need to set .x and .y again.

[code]object.x, object.y = 100, 100

group2:insert(object)
print(object.x, object.y) – still 100,100, but now it’s 100,100 from the perspective of group 2!)

–if group2 is at the bottom of the screen, then object is now bottom of the screen + 100
[/code]

The only time your object has the same coordinates as screen coordinates (ie: 0,0 is top left corner of the display) is if you have it in a group that starts there. Otherwise x/y is basically always relative to the group’s location

EDIT: Also in regard to your original post, all you need to do is the insert.

group2:insert(object) -- moves object from wherever it started to group 2.

There is no information to remove from group1 because a display object can only belong to one group at a time. [import]uid: 41884 topic_id: 27068 reply_id: 110535[/import]

Ok, I got my code back to how it was. Here it is:

storyboard = require( "storyboard" )  
scene = storyboard.newScene()  
sprite = require "sprite"  
movieclip=require ("movieclip")  
ui = require( "ui" )  
physics = require "physics"  
physics.start()  
physics.setGravity( 0,12 )  
  
function scene:createScene( event )  
 local group = self.view  
 local myRect=display.newRect(0,0,1,1)  
 myRect.alpha=0  
 group:insert(myRect)  
end  
  
function scene:enterScene( event )  
 local group = self.view  
  
veryback=display.newGroup()  
back=display.newGroup()  
itemsBox=display.newGroup() --Itemsbox group  
front=display.newGroup()  
front:insert(itemsBox)  
game=display.newGroup() --Master Group, for camera scrolling  
  
local height=display.contentHeight  
local width=display.contentWidth  
local middleY = display.contentHeight/2  
local middleX = display.contentWidth/2 --alignment  
  
local farBorderLeft=display.newRect(0, 0, 10, 768)  
farBorderLeft:setFillColor ( 0, 0, 0 )  
farBorderLeft.x=-7  
physics.addBody ( farBorderLeft, "static", {density=1, friction=1, bounce=0.3})  
local farBorderRight=display.newRect(0, 0, 10, 768)  
farBorderRight:setFillColor ( 0, 0, 0 )  
farBorderRight.x=2055  
physics.addBody ( farBorderRight, "static", {density=1, friction=1, bounce=0.3})  
game:insert(farBorderLeft)  
game:insert(farBorderRight)  
  
local level=display.newRect(0,0,2048,768)  
level.x, level.y=1024, 384  
level:setFillColor(0,0,0)  
game:insert( level )  
  
local ground=display.newRect(0,0,2048,10)  
physics.addBody ( ground, "static", {bounce=0.1, friction=35})  
ground.x, ground.y=1024, 700  
ground.alpha=1  
game:insert(ground)  
  
local tile=display.newRect(0,0,95,95)  
tile:setFillColor(255,0,0)  
tile.x, tile.y=51.5,51.5  
game:insert(tile)  
function moveMe(event)  
 phase=event.phase  
 t=event.target  
  
 if "began"==phase then  
 display.getCurrentStage ( ):setFocus(t)  
 t.isFocus=true  
 elseif t.isFocus then  
 if "moved"==phase then  
 t.x, t.y=event.x, event.y  
 elseif "ended"==phase or "cancelled"==phase then  
 display.getCurrentStage ( ):setFocus(nil)  
 t.isFocus=false  
 if event.y\>119 then --start of alignment function  
 game:insert(t) --put it into the camera scrolling group  
 elseif event.y\<120 then  
 itemsBox:insert(t) --put it in the items box so that it won't scroll  
 --transition.to(t, {x=51.5, y=51.5, time=75 } ) (for animation, so it doesn't look so jerky  
 t.x, t.y=51.5, 51.5  
 end  
 end  
 end  
end  
tile:addEventListener("touch", moveMe)  
  
local player=display.newCircle(0,0,90)  
player.x, player.y=middleX, middleY  
physics.addBody(player, {radius=90, friction=35})  
game:insert(player)  
player.angularVelocity=500  
  
local function moveCamera()  
 if (player.x \> 512 and player.x \< 1536) then  
 game.x = -player.x + 512  
 end  
end  
Runtime:addEventListener( "enterFrame", moveCamera )  
end  
  
function scene:exitScene( event )  
 local group = self.view  
end  
  
function scene:destroyScene( event )  
 local group = self.view  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  

Now, that should be exactly what everyone said to do. However, it does exactly the same thing if you uncomment the transition and comment the flat x and y setting: the tile hides! Why? Help would REALLY be appreciated. As a side note: You can see it best if instead of circles and rectangles you use images. For example, you can see the ball spin and the ground pass by, etc… [import]uid: 147322 topic_id: 27068 reply_id: 110567[/import]

Update:
The object is in the right place. You can touch it and drag it out of the items bar and it will appear when you release the touch. It is just hidden. I tried seeing if it was in the back by calling the function t:toFront and it is not in the back or anything (behind the background rect) [import]uid: 147322 topic_id: 27068 reply_id: 110691[/import]

hello? [import]uid: 147322 topic_id: 27068 reply_id: 110891[/import]

You have a layer stacking problem…

the game group takes up your whole screen. Your items group is behind it.

do a:

front:toFront()

and see if that makes a difference.
I should also point out that you are using storyboard in what I suspect is an attempt to use it for scene management.

None of your display groups are being put into “group”, the scene’s master display group. When the scene exits, nothing is going to be removed from the screen.

Also, why are you doing all of your level creation in enterScene() instead of createScene()? [import]uid: 19626 topic_id: 27068 reply_id: 110894[/import]

@robmiracle:

Thank you so much! My tiles work now. Can you tell me what you mean by “using storyboard for scene management”? And also, thank you for the tip on putting things in the scene’s master group. I was wondering why everything was staying there when I switched scenes. (I am really just mostly fooling around. I am not a very advanced coder)

So should I create all of my objects in createScene() and then start the listeners and timers in enterScene()?

Just to clarify, what I was doing was putting my tile in the group, and the group was in the back? So it wouldn’t matter if I called t:toFront() because it would just move to the front of the group, but not the front of the scene? [import]uid: 147322 topic_id: 27068 reply_id: 110986[/import]

@binc.games

Why are you using storyboard?

I assume it’s because you want to easily have multiple screens/scenes like a help screen, a credits screen, a menu screen etc. That’s why people tend to use storyboard or the 3rd party Director package.

But you are not actually using storyboard in your code above. The framework is there.

In createScene you create a 1x1 square just to solve the crashing problem by not having a display object.

You then do all your work in enterScene() but for storyboard to actually do what it does, you have to add things to the scene’s “view”, which is conveniently assigned to a variable called “group” in each of the createScene/enterScene type functions.

If you DO NOT put your display objects in “group” then storyboard won’t move them/hide them/clean them up/show the next scene for you. You need to then do a:

group:insert(veryback)  
group:insert(back)  
group:insert(game)  
group:insert(front)  

so that storyboard will do what you want it to do for you.

As for the other question, moving a tile to the front will move it to the front of the group. If that group is still hidden behind another group the tile will still be hidden. [import]uid: 19626 topic_id: 27068 reply_id: 110988[/import]