Copy of sprite or Clone of Sprite

Forums Programming cocos2d support (graphics engine) Copy of sprite or Clone of Sprite

This topic contains 6 replies, has 3 voices, and was last updated by  Birkemose 2 years, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
Author Posts
Author Posts
December 29, 2011 at 12:10 pm #238039

precious.logic
@preciouslogic

hi

i have problem with my code i have one class having sprite

class1.H

@interface Class1 : CCNode {

CCSprite *friPanSprite ;

}
@property (nonatomic,copy) CCSprite *friPanSprite;

@end

class1.m

@implementation Class1

@synthesize friPanSprite;

@end

so when i do this in main

CCSprite *lable = [CCSprite spriteWithFile:@"test.png"];
Class1 *objclass = [[Class1 alloc]init];
objclass.friPanSprite = lable;

it got sigarta error but when i change the line

@property (nonatomic,retain) CCSprite *friPanSprite;

to this it worked but i want to copy the ccsprite or want to make the clone of the sprite how can i do this can any one help me thanks in advance

December 29, 2011 at 12:22 pm #360450

marco2D
Participant
@marco2d

to avoid problem like this, if you use friPanSprite in the same .m file you don’t need to declare @property.

Let xcode handle that for you.

December 29, 2011 at 12:27 pm #360451

precious.logic
@preciouslogic

no i want to copy make the copy of the lable and store it to the fripanSprite if i don’t use @property then how to make the copy of the lable sprite tell me that

December 29, 2011 at 12:35 pm #360452

marco2D
Participant
@marco2d

CCTexture2D *tx = [mySprite1 texture];

CCSprite *mySprite2 = [CCSprite spriteWithTexture:tx];

December 29, 2011 at 12:38 pm #360453

marco2D
Participant
@marco2d

if you need a full copy of some object you can try this:

@interface YourClass : NSObject <NSCopying>

{

SomeOtherObject *obj;

}

// In the implementation

-(id)copyWithZone:(NSZone *)zone

{

// We’ll ignore the zone for now

YourClass *another = [[YourClass alloc] init];

another.obj = [obj copyWithZone: zone];

return another;

}

December 29, 2011 at 4:15 pm #360454

precious.logic
@preciouslogic
CCTexture2D *tx = [mySprite1 texture];
CCSprite *mySprite2 = [CCSprite spriteWithTexture:tx];

by this it only get the image of the sprite not all the attribute of the sprite like where is place what is the scale or many other and

@interface YourClass : NSObject <NSCopying>
{
SomeOtherObject *obj;
}

// In the implementation
-(id)copyWithZone:(NSZone *)zone
{
// We'll ignore the zone for now
YourClass *another = [[YourClass alloc] init];
another.obj = [obj copyWithZone: zone];

return another;
}

this is only for that class which i created this is not work for the sprite so any other option

December 29, 2011 at 4:26 pm #360455

Birkemose
Keymaster
@birkemose

You never EVER create member data outside the class. That is what OOP is all about. Encapsulation.

In case you need your class to create a clone, make a method that clones the input.

[ objclass cloneSprite:lable ];

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.