How to change images of sprites whch lie in the touch area on touch event

54 views Asked by At

Here ballList is the list of sprites. On touching the sprites, the image changes.

The problem is that the when two sprites are present in the touch area, i.e. in the list having positions where the sprites are placed for which image change should take place, the image change takes for only one sprite, although the loop runs for the number of sprites, whose positions are there in the list.

How to resolve this?

void OnTouchBegin(List<CCTouch> arg1, CCEvent arg2) {
    var target = arg2.CurrentTarget.BoundingBox;
    location = arg1[0].Location;
    CCRect touchArea = new CCRect(location.X, location.Y, 100, 100);
    // positionList = TouchAreaContainsPoint(location);
    var node = new CCDrawNode();
    if (touchArea.ContainsPoint(location)) { 
        action1 = new CCSequence(new CCCallFunc(Callback1));
        RunActionAsync(action1);
    }
}  

void Callback1() {
    positionList = TouchAreaContainsPoint(location);
    if (positionList.Count > 0) {
        for (i = 0; i < positionList.Count; i++) {
            CCPoint pos = positionList[i];
            spriteEntity.PositionX = pos.X;
            spriteEntity.PositionY = pos.Y;
            this.AddChild(spriteEntity);
        }
    }

    //throw new NotImplementedException();
} 
CCPoint PlacePoints(int i) {
    CCPoint position;
    double x = (26.783879 * 19.692) / 180;
    double y = (75.841477 * 19.692) / 360;
    CCPoint userLocation = new CCPoint((float)x, (float)y);

    position.X = 465  - userLocation.X;
    position.Y = (float)(513.5  - userLocation.Y);
    return position;
    //throw new NotImplementedException();
}
List<CCPoint> TouchAreaContainsPoint(CCPoint location) {
    for (i = 0; i < ballList.Count; i++) {
        CCPoint position = PlacePoints(i);
        float differenceX = Math.Abs(position.X - location.X);
        float differenceY = Math.Abs(position.Y - location.Y);
        if ((Math.Pow(differenceX,2) + Math.Pow(differenceY,2)) < Math.Pow(50.0,2)) {
            positionList.Add(position);
        } else {
            continue;
        }
    }

    return positionList;
    //throw new NotImplementedException();
}
0

There are 0 answers