Forum Libraries, Components, and Plugins

Touch Event on CCDrawNode Problem

RamonNevesRamonNeves USMember

Hello,

I have the following code:

`class Circle : CCDrawNode
{
const int RADIUS = 80;
CCEventListenerTouchAllAtOnce _touchListener;

    public Circle () : base()
    {
    DrawSolidCircle (pos: new CCPoint(0, 0), radius: RADIUS, color:CCColor4B.Gray);
    _touchListener = new CCEventListenerTouchAllAtOnce();
            _touchListener.OnTouchesEnded = Select;
    AddEventListener(_touchListener, this);
}
public void Select(System.Collections.Generic.List<CCTouch> touches, CCEvent touchEvent)
    {
        if (touches.Count > 0)
        {
            if (this.BoundingBoxTransformedToWorld.ContainsPoint(touches[0].Location))
            {
                DrawSolidCircle(pos: new CCPoint(0, 0), radius: RADIUS, color: CCColor4B.Orange);
            } 
        }       
    }

}`

The problem is when i touch the generated circle, the area where actually triggers the Touch Event is not exactly the Bounding Box area.
In the attached image is possible to visualize what is happening.
How can i solve this problem? What is wrong in my code?

Thanks!

Posts

  • RamonNevesRamonNeves USMember

    Problem solved!

    To get around this, i changed couple things:

    • The base class from CCDrawNode to CCNode.
    • At the base constructor, i inserted a CCSize parameter with Radius from circle * 2 on both height and width.
    • Now the circle drawing is a child from the Circle class (CCNode), and i start the drawing from the center of the Parent.
    • The touch event is attached to the CCNode, not the circle anymore.

    Here is the code now:
    `public class Circle : CCNode
    {
    const int RADIUS = 80;

        CCEventListenerTouchOneByOne _touchListener;
        CCDrawNode _circle;
    
        private static CCSize size = new CCSize(RADIUS * 2, RADIUS * 2);
    
        public Circle (int id) : base(size)
        {
            _circle = new CCDrawNode();
            _circle.DrawSolidCircle (pos: new CCPoint(RADIUS, RADIUS), radius: RADIUS, color: CCColor4B.LightGray);
            AddChild(_circle);
    
            _touchListener = new CCEventListenerTouchOneByOne();
            _touchListener.OnTouchBegan = TouchBegan;
            _touchListener.OnTouchEnded = Select;
            AddEventListener(_touchListener, this);
    
        }
    
        public bool TouchBegan(CCTouch touch, CCEvent e)
        {
            return BoundingBoxTransformedToParent.ContainsPoint(touch.Location);
        }
    
        public void Select(CCTouch touch,CCEvent e)
        {
            _circle.DrawSolidCircle(pos: new CCPoint(RADIUS, RADIUS), radius: RADIUS, color: CCColor4B.Orange);
        }
    }
    

    }`

    I hope this info can help somebody else.

Sign In or Register to comment.