Draw a shape beside another

时间:2015-05-12 23:35:04

标签: java android android-canvas

I've drawn a square but how can I place another one beside it without any gaps? I believe the first parameter (0) in <Procedure Name to Execute> needs to change but I don't know what to.

mRedRect1F

Update

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    mRedRect0F = new RectF(0, 0, 50, 50);
    mRedRect1F = new RectF(0, 0, 50, 50);

    canvas.drawRect(mRedRect0F, mRedRectPaint);
    canvas.drawRect(mRedRect1F, mRedRectPaint);

}

2 个答案:

答案 0 :(得分:1)

There are 3 ways you could accomplish this off the top of my head.

The first is to define the second class GameScene: SKScene { let actionID = "TouchActionID" override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { let wait = SKAction.waitForDuration(0.5) let block = SKAction.runBlock { println("Hello World") } let sequence = SKAction.sequence([wait, block]) // Use `actionID`, defined earlier, as the key. // This allows you to cancel the `SKAction`. self.runAction(SKAction.repeatActionForever(sequence), withKey: actionID) } // Cancel the SKAction if the touch ended or was cancelled. Cancelling // could occur if the application resigns being active, for example. // See UIResponder documentation for more information. override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) { self.removeActionForKey(actionID) } override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { self.removeActionForKey(actionID) } } as 50 pixels further to the right.

RecfF

The next is to translate the canvas before drawing the second.

new RectF(50, 0, 100, 50);

And the 3rd is to offset the RectF before drawing.

mRedRect = new RectF(0, 0, 50, 50);
canvas.drawRect(mRedRect, mRedRectPaint);
canvas.translate(50, 0);
canvas.drawRect(mRedRect, mRedRectPaint);

答案 1 :(得分:1)

您可以创建一个toRightOf方法。当然这将是更多的工作,但如果你再次需要做这种事情,你已经有了编写的方法来做到这一点。我现在不在电脑前,或者我会尝试一下。如果您不希望这样做多次,我会使用第一个答案中公布的选项之一。