我正在使用Sprite Kit編寫基於物理學的遊戲。我能夠處理主角和加電之間的聯繫。正如預期的那樣,屏幕上的電源消失,我的型號被更新並播放聲音。但是,角色在接通電源時會改變方向。這是不希望的。我希望讓角色能夠通過權力,而不會因碰撞而改變角色。這裏是我的一些代碼,讓我知道如果你需要更多的幫助。Sprite Kit - 字符和通電碰撞/接觸檢測,不改變字符方向
所有這些都是爲Xamarin.iOS編寫的。但是代碼與Objective-C計數器部分幾乎完全相同。讓我知道是否需要進一步解釋。並提前感謝您的幫助!
編輯:此外,這是在參考了遊戲,我在這個問題討論了建設:
Sprite Kit - Create arch path for SKSpriteNode given a swipe gesture
**的代碼! **
我的碰撞位枚舉:
public enum COLLISION_BITS : uint
{
Character = 1 << 0,
Background = 1 << 1,
Ground = 1 << 2,
BadGuy = 1 << 3,
PowerUp = 1 << 4,
Fan = 1 << 5
}
我的角色對象設置:
public Character (SizeF size, PointF position) : base("character")
{
this.Name = CHARACTER_NAME;
this.Size = size;
this.Position = position;
this.PhysicsBody = SKPhysicsBody.BodyWithRectangleOfSize (this.Size);
this.PhysicsBody.Dynamic = true;
this.PhysicsBody.AffectedByGravity = true;
this.PhysicsBody.AllowsRotation = false;
this.PhysicsBody.Mass = 1.0f;
this.PhysicsBody.SetCategoryBitMask (COLLISION_BITS.Character);
this.PhysicsBody.SetContactTestBitMask (COLLISION_BITS.Ground);
this.PhysicsBody.SetContactTestBitMask (COLLISION_BITS.BadGuy, false);
this.PhysicsBody.SetContactTestBitMask (COLLISION_BITS.PowerUp, false);
this.PhysicsBody.SetContactTestBitMask (COLLISION_BITS.Fan, false);
}
我的上電對象設置:
private void SetupPowerUp(SizeF size, PointF position)
{
this.Size = size;
this.Position = position;
this.PhysicsBody = SKPhysicsBody.BodyWithRectangleOfSize (this.Size);
this.PhysicsBody.Dynamic = false;
this.PhysicsBody.AffectedByGravity = false;
this.PhysicsBody.AllowsRotation = false;
this.PhysicsBody.Mass = 0f;
this.PhysicsBody.SetCategoryBitMask (COLLISION_BITS.PowerUp);
}
,我把我的位擴展方法處理:
public static void SetContactTestBitMask(this SKPhysicsBody body, COLLISION_BITS bits, bool overwrite = true)
{
if (body == null) return;
if (overwrite) {
body.ContactTestBitMask = (uint)bits;
} else {
body.ContactTestBitMask |= (uint)bits;
}
}
public static void SetCategoryBitMask(this SKPhysicsBody body, COLLISION_BITS bits, bool overwrite = true)
{
if (body == null) return;
if (overwrite) {
body.CategoryBitMask = (uint)bits;
} else {
body.CategoryBitMask |= (uint)bits;
}
}
public static bool IsOfCategory(this SKPhysicsBody body, COLLISION_BITS bits, bool matchAll = false)
{
if (body == null) return false;
if (matchAll) {
return (body.CategoryBitMask & (uint)bits) == (uint)bits;
} else {
return (body.CategoryBitMask & (uint)bits) > 0;
}
}
最後,我DidBeginContact方法:
public void HandleDidBeginContact(object sender, EventArgs args)
{
var contact = (SKPhysicsContact)sender;
SKPhysicsBody firstBody, secondBody;
if (contact.BodyA.CategoryBitMask < contact.BodyB.CategoryBitMask) {
firstBody = contact.BodyA;
secondBody = contact.BodyB;
} else {
firstBody = contact.BodyB;
secondBody = contact.BodyA;
}
// collected power up
if (firstBody.IsOfCategory(COLLISION_BITS. Character) && secondBody.IsOfCategory(COLLISION_BITS.PowerUp)) {
secondBody.Node.RemoveFromParent();
GameModel.GatheredPowerUp();
PowerUpCollisionSoundPlayer.Play();
}
// other collision stuff for the other objects in the screen...
// ...
}
完美,就是這樣。我假設collisionBitMask的默認值是它不會與任何東西相沖突。儘管如此,仍然對collisionBitMask和contactTestBitMask的區別有點困惑。但是這幫助我讓它工作。謝謝! – Mike