Hi there, it's meggie! I'm making a packing game, and I have two containers, one called the playArea and one called tile. Tile is where the items are spawned and playArea is the box where the items are to be placed. The tile container sits above the playArea container, like a the head of a snowman made up of two boxes. I would like the player to be able to freely move the items between the two containers. playArea also snaps the items to the nearest 30px box inside. I have some code that seems to work for the x axis. -
if (item.x < item.width/2 && item.x>-50){
item.x = item.width/2
} else if (item.x > bentoW-20-item.width/2 && item.x< bentoW+50){
item.x = bentoW-20-item.width/2
} else if (item.x <-50 && item.x > -240){
item.addTo(tile)
item.alp(1)
zog("moving to tile a")
return
} else if (item.x > bentoW+50) {
item.addTo(tile)
item.alp(1)
zog("moving to tile b")
return
} else {
item.loc(Math.round((item.x-item.width/2)/gridBlock)*gridBlock+item.width/2, Math.round((item.y-item.height/2)/gridBlock)*gridBlock+item.height/2, playArea)
S.update()
}
I tried to replicate it for the y-axis as follows
if (item.y < item.height/2 && item.y > -40){
item.y = item.height/2
} else if (item.y > bentoH-20-item.height/2){
item.y = bentoH-20-item.height/2
} else if (item.y < -40){
item.addTo(tile)
zog("moving to tile c")
item.alp(1)
return
} else {
item.loc(Math.round((item.x-item.width/2)/gridBlock)*gridBlock+item.width/2, Math.round((item.y-item.height/2)/gridBlock)*gridBlock+item.height/2, playArea)
S.update()
}
I set the alpha so that when the item is in the playArea, it is 0.1 and when it is in the tile it is 1. What's happening is that when I try to move an item out of the box from the top of the box, for the first instance of it, the item will just drop where I pressup (which is what I want), but the rest of the time, the item will return to an alpha of 1, which seems to be indicating that it is in the tile container, but it will snap back to some location above the playArea.
Both of these blocks of code exist in the same pressup function for the playArea
I suspect that there might be a problem with the changing coordinates between the two tiles, but if that were the case, shouldn't the x part of the code not be working as well?