1

I'm testing CocosSharp on Visual Studio 2017, I want to perform a zoom on a CCLayer by testing a List of CCTouch elements.

This list is sended by a CCEventListenerTouchAllAtOnce.OnTouchesBegan event.

Here is my code that doesn't work :

    private void HandleTouchBegan(List<CCTouch> touches, CCEvent touchEvent)
    {
        if (touches.Count == 2)
        {
            CCTouch t1 = touches[0];
            CCTouch t2 = touches[1];

            CCPoint Loc1 = t1.LocationOnScreen;
            CCPoint Loc2 = t2.LocationOnScreen;

            CCPoint PreviousLoc1 = t1.PreviousLocationOnScreen;
            CCPoint PreviousLoc2 = t2.PreviousLocationOnScreen;

            double CurrentDistance = Math.Sqrt(Math.Pow(Loc1.X - Loc2.X, 2.0f) + Math.Pow(Loc1.Y - Loc2.Y, 2.0f));
            double PreviousDistance = Math.Sqrt(Math.Pow(PreviousLoc1.X - PreviousLoc2.X, 2.0f) + Math.Pow(PreviousLoc1.Y - PreviousLoc2.Y, 2.0f));

            double Delta = CurrentDistance - PreviousDistance;

            layer.ScaleX += (float)Delta;
            layer.ScaleY += (float)Delta;
       }

I added the touch listener like this on the CCLayer layer:

        var touchListener = new CCEventListenerTouchAllAtOnce();
        touchListener.OnTouchesBegan = HandleTouchBegan;
        layer.AddEventListener(touchListener);

I get the event with 2 CCTouch in the touches list, but the layer isn't scaled.

Where am I wrong ? Is there a better way to do this layer zoom ?

Thank You for your help.

Alexus
  • 276
  • 1
  • 5
  • 22

1 Answers1

0

I just started with this stuff and came across your post, so I'm not an authority at all.

I took your code and used OnTouchesMoved instead. Then I divided the delta by 100, because it was much to big as it is in this code.

Now it zooms in/out, but in an odd "sideways" kind of way. More work to be done, but thought I'd give some input!

rythos42
  • 1,217
  • 2
  • 11
  • 27