Bin*_*noy 3 android xamarin cocossharp
我的屏幕底部有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;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码将所有图像一次移动到触摸的坐标.我的要求是一次拖动一个图像不同于一次.在我看来,Xamarin和Github中给出的Cocossharp API和教程没那么有用.有没有一种方法可以在一个触摸实例上拖动一个图像?帮助赞赏
下面是一个创建两个精灵的示例,您可以单独拖动它们.
设计:
备注:
一旦找到感兴趣的精灵,就会返回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
}
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
601 次 |
| 最近记录: |