I've got a CCSprite on a CCLayer and scrolling is handled by a CCFollow action.
I'm trying to detect a touch on the CCSprite, but I cant find the magic incantation to detect it reliably when the screen has scrolled. I'm not sure if I should be adding the CCEventListenerTouchOneByOne to the sprite, to the layer, what the 2nd optional parameter is when I create the CCEventListenerTouchOneByOne. Touch location seems to be in relation to the screen. The sprite will always be topmost, so I'm not worried about it being behind other nodes.
I must be missing something obvious, but Ive been Console.Writelining out positions and bounding rects of every conversion I can think of, and I'm losiong the will to live now!
Anyone any handy pointers?
Posts
Neil
Can you put together a small project for us to take a look at. That will help with tracking where the problem is.
Using CocosSharp 1.6, this relies on a file called Sprite.png (mine is 100x100Px). You should be able to just copy/paste this into a new project.
`using CocosSharp;
namespace CocosTouchIssue
{
class AppDelegate : CCApplicationDelegate
{
public override void ApplicationDidFinishLaunching(CCApplication application, CCWindow mainWindow)
{
base.ApplicationDidFinishLaunching(application, mainWindow);
var scene = new CCScene(mainWindow);
var layer = new CCLayerColor(CCColor4B.AliceBlue);
scene.AddChild(layer);
var sprite = new CCSprite(new CCTexture2D("Sprite.png"));
layer.AddChild(sprite);
var follow = new CCFollow(sprite, new CCRect(0, 0, 3000, 600));
layer.RunAction(follow);
}
`
In fact - here's the file so you can just add it to a new project....(I'd missed the attach file option!)
(Edit: File wouldnt upload)
Neil
Try something like this your
touchEvent.OnTouchBegan = (touch, _event) =>
delegate:Thanks Kenneth - that worked perfectly. Was there a reference point for creating that or was it just through experience?
Many thanks!
@NeilDanson : you can use this by using BoundingBoxTransformedToParent.ContainsPoint(). ballSprite.BoundingBoxTransformedToParent.ContainsPoint(arg1.Location) in this manner in the HandleTouchBegan Method or in case you face any problem try to create separate listeners on the layer.