gpt4 book ai didi

ios - 如何进行脉冲物理学

转载 作者:行者123 更新时间:2023-11-29 03:07:30 24 4
gpt4 key购买 nike

好吧伙计们,我有两个 Sprite ,一个是敌人,当元素出现时,它会在 map 周围移动以捡起元素,它使用 SKActions 来做到这一点,然后我还有他不应该穿过的墙。反正。我要做的就是让他在撞到墙上时弹开。我该怎么做?

代码如下:

//对于敌人墙我有这个代码:

- (void) EnemyBelongings {
EnemyWall = [SKSpriteNode spriteNodeWithImageNamed:@"EnemyWall@2x"];
EnemyWall.name = @"hisWall";
EnemyWall.position = CGPointMake(512, 260);
EnemyWall.xScale = 0.09;
EnemyWall.yScale = 0.09;
EnemyWall.physicsBody.categoryBitMask = PhysicsCategoryEnemyWall;
EnemyWall.physicsBody.contactTestBitMask = PhysicsCategoryEnemy;
EnemyWall.physicsBody.collisionBitMask = 0;
[self addChild:EnemyWall];

//对于敌人角色我有这个代码

- (void) Enemy {
_Enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy"];
_Enemy.position = CGPointMake(520, _Enemy.size.height/1.50);
_Enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width];
_Enemy.physicsBody.usesPreciseCollisionDetection = YES;
_Enemy.physicsBody.categoryBitMask = PhysicsCategoryEnemy;
_Enemy.physicsBody.contactTestBitMask = PhysicsCategoryEnemyWall;
_Enemy.physicsBody.collisionBitMask = 0;
[self addChild:_Enemy];
}

//对于敌人的移动我有这段代码

- (void) EnemySpawner {
[ForEnemy runAction:appear2 completion:^{
SKAction *wait = [SKAction waitForDuration:1.0];
[_Enemy runAction:wait];
SKAction *actionXMove2 = [SKAction moveToX:ForEnemy.position.x duration:0.14];
SKAction *actionYMove2 = [SKAction moveToY:ForEnemy.position.y duration:0.14];
[_Enemy runAction:actionYMove2];
[_Enemy runAction:actionXMove2];
}];

-(void)didBeginContact:(SKPhysicsContact *)contact
{
if (contact.bodyA.categoryBitMask == PhysicsCategoryEnemy && contact.bodyB.categoryBitMask
== PhysicsCategoryEnemyWall)
{
SKNode *enemy = contact.bodyA.node; // a is the enemy here
// Code here
SKNode *wall = contact.bodyB.node; // b is the enemy's wall here
// Code here
}
else if (contact.bodyB.categoryBitMask == PhysicsCategoryEnemy &&
contact.bodyA.categoryBitMask == PhysicsCategoryEnemyWall)
{
SKNode *enemy = contact.bodyB.node;
// Code here
SKNode *wall = contact.bodyA.node;
// Code here
}

最佳答案

您需要在节点的physicsBody上applyImpulse使其自然模拟并与其他physicsBody交互。

请看我的回答here关于如何做同样的事情。

tutorial 复制 rwAdd、rwSub、rwMult、rwLength 和 rwNormalize 方法作者:雷文德利希。

然后,尝试使用此代码:

[ForEnemy runAction:appear2 completion:^{
SKAction *wait = [SKAction waitForDuration:1.0];

CGPoint offset = rwSub(location, ForEnemy.position);
CGPoint direction = rwNormalize(offset);
float forceValue = 200; //Edit this value to get the desired force.
CGPoint shootAmount = rwMult(direction, forceValue);

CGVector impulseVector = CGVectorMake(shootAmount.x, shootAmount.y);

[_Enemy.physicsBody applyImpulse:impulseVector];


}];

关于ios - 如何进行脉冲物理学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com