iosobjective-ccocos2d-iphoneccspriteccscrolllayer

Creating a scrollview


Here's my current situation: I created a CCScene named StoreScene. Within that scene, I've initialized a CCLayer and just named it Store layer.

Now, I want a scrollable layer that contains content which the user can touch. To do this, I created another CCLayer named Store Container. I create an instance of this layer and add it as a child to StoreScene.

In StoreContainer, I've added multiple sprites, each with a unique tag. The scrolling is done within the StoreScene and touches will move the entire Storecontainer layer up or down.

I have added 4 sprites to my scrolling layer (store container) Initially, sprite 1 is located at 0,10 sprite 2 is located at 0,20 sprite 3 is located at 0,30 sprite 4 is located at 0,40

Obviously, as the entire storecontainer layer shifts, the sprite positions shift as well. However, when I do this in the TouchesEnded method: if (CGRectContainsPoint(sprite1.boundingBox, touchpoint)){ NSLog(@"TouchedSprite1"); } ... and so on for each sprite

The touch locations of each sprite remain in the same place!!! Visually, the sprites are moving up and down nicely. but their locations when receiving touches stay constant.. is there a reason for this? Any other way for approaching a scrolling layer? I've already looked at the UIKit's scrollview and I've looked at CCScrollLayer and both aren't good enough for me. My way may be simpler but it doesn't work as planned. Thanks in advance! ^_^


Solution

  • I'm guessing you've implemented touchesEnded in your StoreContainer. In that case the touches' coordinates will be relative to that layer, which explains why their coordinate system is following the layer around the screen. You could implement touchesEnded in the scene instead, or put the StoreContainer instance inside a new and immobile layer whose purpose is only to handle touches.

    Alternatively you could keep your current setup and use your answer to this question to get the touch coordinates in the world... :)