我想在以下幾點來安排我的精靈點迭代公式:發現使用for循環排成三角形
ccp(240.0, 160.0);
ccp(300.0, 120.0);
ccp(300.0, 200.0);
ccp(360.0, 80.0);
ccp(360.0, 160.0);
ccp(360.0, 240.0);
我試圖獲得使用for循環這些點的迭代公式。我已經有一段時間了。以下是點的視覺表示。請幫忙。
*
*
* *
*
*
我想在以下幾點來安排我的精靈點迭代公式:發現使用for循環排成三角形
ccp(240.0, 160.0);
ccp(300.0, 120.0);
ccp(300.0, 200.0);
ccp(360.0, 80.0);
ccp(360.0, 160.0);
ccp(360.0, 240.0);
我試圖獲得使用for循環這些點的迭代公式。我已經有一段時間了。以下是點的視覺表示。請幫忙。
*
*
* *
*
*
你想要這樣的東西嗎? (其快速,草圖只)
假設初始參數:
initPoint (x0, y0)
initVector (vX,vY)
num of iteration c
index = c
while index
for(j = index, currentPoint = initPoint+(c-index)*(0,2*vY); j;j--, currentPoint += initVector)
draw currentPoint
index--
基本上主要的想法是,你從右上方點開始,並移動與初始向量作爲圖中的上下文很多時候,隨着迭代的進行(到左下角)並繪製點。然後將初始點沿y軸向下移動並重復減一次。
這裏有一種方法:
我沒有花,但一兩分鐘思考它,但它可以更容易,如果你從最大排開始,然後一路下跌:
PatternTest .H
#import "cocos2d.h"
@interface PatternTest : CCLayer
@end
PatternTest.m
@implementation PatternTest
-(id) init
{
if((self=[super init]))
{
CCNode *grid = [self generateArrowPatternWithBaseRowOfNumSprites:5 spacedApart:ccp(25.0f, 25.0f)];
[grid setPosition:ccp(50.0f,50.0f)];
[self addChild:grid];
}
return self;
}
-(CCNode *) generateArrowPatternWithBaseRowOfNumSprites:(float) numSprites spacedApart:(CGPoint) space
{
CCNode* patternNode = [CCNode node];
CGPoint tempPos = ccp(0.0f, 0.0f);
float offset = 0.0f;
while (numSprites > 0)
{
for(int x=numSprites;x>0;x--)
{
CCSprite *patternSprite = [CCSprite spriteWithFile:@"Icon.png"];
[patternSprite setScale: 0.3f];
[patternSprite setPosition: tempPos];
[patternNode addChild:patternSprite];
tempPos = ccpAdd(tempPos, ccp(0.0f,space.y));
}
tempPos = ccp(tempPos.x, 0.0f);
offset = offset + (space.y/2.0f);
tempPos = ccpAdd(tempPos, ccp(space.x, offset));
numSprites -= 1;
}
return patternNode;
}
@end
你想用for循環生成點嗎?如果是這樣,這是一個壞主意,不值得花時間。如果您只需遍歷點數組,則需要將點存儲在數組中。 – zakhej 2013-04-25 13:38:50