Cocos2d iphone 从一个类访问另一个类中的数组-cocos2d
这听起来很简单。我在一个类(Sprites.mm)中用physicsprites填充了一个数组,该类返回自身(我在init方法HelloWorldLayer.mm类中初始化)。如何在HelloWorldLayer.mm的更新方法中访问数组(从Sprites.mm)?我想对更新方法中的精灵设置一些限制。请提供帮助。在Sprites.h文件中创建属性Cocos2d iphone 从一个类访问另一个类中的数组-cocos2d,cocos2d-iphone,nsmutablearray,box2d-iphone,Cocos2d Iphone,Nsmutablearray,Box2d Iphone,这听起来很简单。我在一个类(Sprites.mm)中用physicsprites填充了一个数组,该类返回自身(我在init方法HelloWorldLayer.mm类中初始化)。如何在HelloWorldLayer.mm的更新方法中访问数组(从Sprites.mm)?我想对更新方法中的精灵设置一些限制。请提供帮助。在Sprites.h文件中创建属性 @property (nonatomic, readonly) NSArray* physicSprites; 然后在你的.mm文件中 @synth
@property (nonatomic, readonly) NSArray* physicSprites;
然后在你的.mm文件中
@synthesize physicSprites = m_physicSprites;
如果您的阵列实例称为m_PhysicScrites
然后你就可以像往常一样访问它了
[spritesInstance physicSprites];
或
首先,您需要在两个类中共享相同的b2World,然后您可以访问Sprites.mm world 为了更好的理解,我制作了一个演示。我正在添加Sprites.mm代码,我称之为Spritese.h&Spritese.mm 下面是Spritese.h代码
@interface Spritese : CCLayer {
NSMutableArray *arrSprite;
b2World* world;
}
@property (nonatomic,retain) NSMutableArray *arrSprite;
-(id)initWithArrayOfSprites : (b2World *)_world;
@end
下面的一个用于.mm
@implementation Spritese
@synthesize arrSprite;
#define PTM_RATIO 32
#define kTagBatchNode 1
-(id)initWithArrayOfSprites :(b2World *)_world{
if((self = [super init])){
CGSize screenSize = [CCDirector sharedDirector].winSize;
world = _world;
//Set up sprite
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"blocks.png" capacity:150];
[self addChild:batch z:0 tag:kTagBatchNode];
for(int i=0; i<3; i++)//creating 3 objects
[self addNewSpriteWithCoords:ccp(screenSize.width/2, screenSize.height/2)];
}
return self;
}
-(void) addNewSpriteWithCoords:(CGPoint)p
{
CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y);
CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [self getChildByTag:kTagBatchNode];
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
int idx = (CCRANDOM_0_1() > .5 ? 0:1);
int idy = (CCRANDOM_0_1() > .5 ? 0:1);
CCSprite *sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(32 * idx,32 * idy,32,32)];
[batch addChild:sprite];
sprite.position = ccp( p.x, p.y);
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
[arrSprite addObject:sprite];
}
@end
并将其合成到.mm文件中
现在在HelloWorld的init方法中添加以下代码
sprit = [[Spritese alloc] initWithArrayOfSprites:world];
[self addChild:sprit];
最后,在tick或update方法中,您需要添加
-(void) tick: (ccTime) dt
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(dt, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
Spritese *s = (Spritese *)b->GetUserData();
for(int i=0; i < [sprit.arrSprite count]; i++){
if(s == [sprit.arrSprite objectAtIndex:i]){
s.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
s.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
NSLog(@"Process Sprite Here");
}
}
}
}
}
-(无效)勾选:(ccTime)dt
{
int32速度迭代=8;
int32=1;
世界->步长(dt、速度迭代、位置迭代);
对于(b2Body*b=world->GetBodyList();b;b=b->GetNext())
{
如果(b->GetUserData()!=NULL){
CCSprite*myActor=(CCSprite*)b->GetUserData();
myActor.position=CGPointMake(b->GetPosition().x*PTM_比率,b->GetPosition().y*PTM_比率);
myActor.rotation=-1*CC_弧度到_度(b->GetAngle());
精灵*s=(精灵*)b->GetUserData();
for(int i=0;i<[sprit.arrSprite count];i++){
if(s==[sprit.arrSprite objectAtIndex:i]){
s、 position=CGPointMake(b->GetPosition().x*PTM_比率,b->GetPosition().y*PTM_比率);
s、 旋转=-1*CC_弧度到_度(b->GetAngle());
NSLog(@“此处处理精灵”);
}
}
}
}
}
我希望代码示例能在您这边起作用。我想它起作用了,但我有一个问题,我想是更新方法引起的。现在我想在say
arrSprite
中对精灵施加重力(高于世界),但它们仍然对世界重力做出响应。请参阅下面的更新方法<代码>-(无效)更新:(ccTime)dt{int32 velocityIterations=8;int32 positionIterations=1;world->Step(dt,velocityIterations,positionIterations);for(sprit.arrSprite中的PhysicsSprite*spr){b2Body*bBody=[spr getPhysicsBody];bBody->ApplyForce(b2Vec2(0,20*bBody->GetMass()),bBody->GetWorldCenter();})当您在两个图层中共享同一个世界时。你无法区分这两个世界。我想任何一层都不能有超过一个世界。请你给我进一步解释一下,我不完全理解。谢谢抱歉,我无法解释更多,因为我只知道它发生的原因,并得出结论,它产生冲突是因为共享同一个世界。
sprit = [[Spritese alloc] initWithArrayOfSprites:world];
[self addChild:sprit];
-(void) tick: (ccTime) dt
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(dt, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
Spritese *s = (Spritese *)b->GetUserData();
for(int i=0; i < [sprit.arrSprite count]; i++){
if(s == [sprit.arrSprite objectAtIndex:i]){
s.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
s.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
NSLog(@"Process Sprite Here");
}
}
}
}
}