【问题标题】:CGContextAddArc: how to create circle?CGContextAddArc:如何创建圆?
【发布时间】:2013-11-03 20:21:38
【问题描述】:

我开始使用新的 Spritekit 框架在 Xcode 中开发我的塔防项目,我想在玩家将创建的任何塔周围添加一个可见的半径(圆)。

所以我创建了以下代码:

// Place Towers
-(void)mouseUp:(NSEvent *)theEvent {
    /* Called when a mouse click occurs */

    CGPoint location = [theEvent locationInNode:self];

    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"tower"];

    sprite.position = location;
    sprite.scale = 1.5;

    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextAddArc(myContext, location.x, location.y, 10, 0, M_PI*2, YES);
    CGContextSetRGBStrokeColor(myContext, 0, 0, 225, 0);

    [self addChild:sprite];

}

我不熟悉角度和其他东西,因为我是第一次这样做,所以我只是猜测它应该是这样的。我很确定我错过了什么。

感谢您的任何建议(我等待一些很好的批评)。

【问题讨论】:

  • 它是否产生了预期的结果?如果没有,你会得到什么结果,你期望什么? - “我只是猜测应该是这样的”根本不是问题!
  • 它不会产生任何东西,只是精灵本身,没有圆圈.. 对此感到抱歉,1. 时间在这里发布一些东西..

标签: objective-c macos quartz-graphics sprite-kit


【解决方案1】:

直接来自 SpriteKit 编程指南:

SKShapeNode *ball = [[SKShapeNode alloc] init];

CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);
ball.path = myPath;

ball.lineWidth = 1.0;
ball.fillColor = [SKColor blueColor];
ball.strokeColor = [SKColor whiteColor];
ball.glowWidth = 0.5;

【讨论】:

  • 我尝试了上面的代码(改变了位置),系统化了 myPath 等等。什么都没有:/可能是我的背景图片覆盖了它吗?
  • 我尝试了 NSLog(@"Ball created!") 如果函数运行,它确实可以运行,但它根本没有显示任何内容。
  • 我想通了 :D 忘了 [self addChild:ball]
【解决方案2】:

使用CGContextStrokeEllipseInRect() 可能会更容易。所以如果你想要一个半径为 10 的圆,以 (x,y) 为中心,它会是:

const CGFloat color[] = { 0, 0, 1, 1 }; // Alpha should be 1, not 0!
CGContextSetStrokeColor (myContext, color); 
CGContextStrokeEllipseInRect (myContext, CGRectMake(x - 10, y - 10, 20, 20));

【讨论】:

  • 是的,它应该,我不知道我是否按照你的意思理解,所以如果你可以发布一些代码或任何东西?会很棒
  • 实际上,我刚刚意识到有一个CGContextStrokeEllipseInRect() 可以一次性完成所有操作!
猜你喜欢
  • 1970-01-01
  • 2019-12-24
  • 2020-05-25
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
相关资源
最近更新 更多