Android游戏:一次将一组图像从一组图像拖到屏幕上 [英] Android game : Drag one image at a time into screen from a group of images

查看:96
本文介绍了Android游戏:一次将一组图像从一组图像拖到屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕底部堆叠了5张图像.我的游戏的目的是拖动这些图像并在特定条件下将它们连接起来.(拼图游戏的排序) 我使用了以下代码

I have 5 images stacked in the bottom of my screen. My game's aim is to drag these images and connect them on certain conditions.(Sort of jigsaw puzzle) I used the following code

var touchListener = new CCEventListenerTouchAllAtOnce ();
touchListener.OnTouchesEnded = OnTouchesEnded;
touchListener.OnTouchesMoved = HandleTouchesMoved;
AddEventListener (touchListener, this);
void HandleTouchesMoved (List touches, CCEvent touchEvent)
{
    foreach(var tap in touches)
    {
       var locationOnScreen = tap.Location;
       alarmicSprite.PositionY = locationOnScreen.Y;
       alarmicSprite.PositionX = locationOnScreen.X;
       pressSwitchSprite.PositionY = locationOnScreen.Y;
       pressSwitchSprite.PositionX = locationOnScreen.X;
     }
}

此代码将所有图像立即移动到触摸的坐标.我的要求是一次拖动一张图像,而不是一次拖动所有图像.在我看来,Xamarin和Github中提供的Cocossharp API和教程并不是那么有用. 是否有一种方法可以在一个触摸实例上拖动一个图像? 感谢帮助

This code moves all images at once to the touched coordinates. My requirement is to get one image dragged at a time unlike all at once. Cocossharp API and tutorials given in Xamarin and Github in my mind is not that helpful. Is there a method which allows to drag one image on one touch instance? Help appreciated

推荐答案

下面是一个示例,该示例创建两个精灵,并允许您分别拖动它们.

Here is an example that creates two sprites and lets you drag them individually.

设计:

  • 您需要检测正在触摸哪个精灵,然后才移动该精灵.
  • 在OnTouchesBegan中保存要触摸的精灵
  • 在OnTouchesMoved中移动当前触摸的精灵

注释:

  • 为每个注册事件的 EVERY 精灵调用一次OnTouchesBegan.因此,如果20个精灵添加了侦听器,则OnTouchesBegan事件将被调用20次(除非被吞咽,请参见下文)
  • 通过编程,您可以确定触摸位置是否在调用OnTouchesBegan的精灵的边界框内.但是,吞咽"触摸将停止对它的所有剩余排队呼叫.要吞下,只需返回true.
  • 找到感兴趣的子画面后,将true返回吞下"触摸事件,并阻止其余的子画面回叫.这样可以节省cpu并更快地执行OnTouchesMoved.在处理完OnTouchesBegan之前,系统将不会调用OnTouchesMoved.

  • The OnTouchesBegan is called once for EVERY sprite that registers for the event. So if 20 sprites add the listener, the OnTouchesBegan event is called 20 times (unless swallowed, see below)
  • Programmatically you determine if the touch position is inside the bounding box of the sprite calling OnTouchesBegan. However "Swallowing" the touch will stop any remaining queued up calls to it. To swallow, just return true.
  • Once you find the the sprite of interest, you return true to "swallow" the touch event and stop the rest of the sprites from calling back. This saves cpu and executes your OnTouchesMoved sooner. The system will not call OnTouchesMoved until it is completely done dealing with OnTouchesBegan.

CCSprite currentSpriteTouched;
CCSprite Sprite1;
CCSprite Sprite2;
protected override void AddedToScene()
{
    base.AddedToScene();

    // Use the bounds to layout the positioning of our drawable assets
    CCRect bounds = VisibleBoundsWorldspace;

    Sprite1 = new CCSprite("redball.png");
    Sprite2 = new CCSprite("blueball.png");

    Sprite1.Position = bounds.Center;
    Sprite2.Position = bounds.Center;
    AddChild(Sprite1);
    AddChild(Sprite2);

    // Register for touch events
    var touchListener =  new CCEventListenerTouchOneByOne();
    touchListener.IsSwallowTouches = true;
    touchListener.OnTouchBegan = this.OnTouchesBegan;
    touchListener.OnTouchMoved = this.OnTouchesMoved;

    AddEventListener(touchListener, Sprite2);
    AddEventListener(touchListener.Copy(), Sprite1);

}


void OnTouchesMoved(CCTouch touch, CCEvent touchEvent)
{
    if (currentSpriteTouched != null)
    {
        currentSpriteTouched.Position = touch.Location;
    }
}

bool OnTouchesBegan(CCTouch touch, CCEvent touchEvent)
{
    // This is called once for each sprite
    // To stop the remaining sprites from calling, 
    //   "swallow" the touch by returning true
    CCSprite caller = touchEvent.CurrentTarget as CCSprite;
    currentSpriteTouched = null;
    if (caller == Sprite1)
    {
        if (Sprite1.BoundingBoxTransformedToWorld.ContainsPoint(touch.Location))
        {
            System.Diagnostics.Debug.WriteLine("Sprite 1 touched ");
            currentSpriteTouched = Sprite1;
            return true;  // swallow    
        }
        else
        {
            return false;  // do not swallow and try the next caller
        }

    }
    else if (caller == Sprite2)
    {
        if (Sprite2.BoundingBoxTransformedToWorld.ContainsPoint(touch.Location))
        {
            currentSpriteTouched = Sprite2;
            System.Diagnostics.Debug.WriteLine("Sprite 2 touched ");
            return true;  // swallow    
        }
        else
        {
            return false;  // do not swallow and try the next caller
        }
    }
    else
    {
        // something else touched
        System.Diagnostics.Debug.WriteLine("Something else was touched");
        return false;  // Do not swallow
    }
}

这篇关于Android游戏:一次将一组图像从一组图像拖到屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆