Android游戏:将一个图像一次拖动到一组图像的屏幕中

Android游戏:将一个图像一次拖动到一组图像的屏幕中,第1张

概述我的屏幕底部有5张图像.我的游戏的目的是拖动这些图像并在特定条件下连接它们.(拼图游戏的排序)我使用了以下代码vartouchListener=newCCEventListenerTouchAllAtOnce();touchListener.OnTouchesEnded=OnTouchesEnded;touchListener.OnTouchesMoved=HandleTouchesMov

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

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和教程没那么有用.
有没有一种方法可以在一个触摸实例上拖动一个图像?
帮助赞赏

解决方法:

下面是一个创建两个精灵的示例,您可以单独拖动它们.

设计:

>您需要检测正在触摸的精灵,然后才移动该精灵.
>保存OntouchesBegan中触摸的精灵
>在OntouchesMoved中移动当前触摸的精灵

笔记:

>为注册事件的每个精灵调用一次OntouchesBegan.因此,如果20个精灵添加了监听器,则OntouchesBegan事件被调用20次(除非被吞下,见下文)
>以编程方式确定触摸位置是否在调用OntouchesBegan的精灵的边界框内.然而,“吞咽”触摸将停止任何剩余的排队呼叫.吞下去,就回归真.
>一旦找到感兴趣的精灵,就会返回true以“吞下”触摸事件并停止其他精灵的回调.这样可以节省cpu并更快地执行OntouchesMoved.在完成处理OntouchesBegan之前,系统不会调用OntouchesMoved.

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游戏:将一个图像一次拖动到一组图像的屏幕中全部内容,希望文章能够帮你解决Android游戏:将一个图像一次拖动到一组图像的屏幕中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1107982.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存