0
votes

I'v got trouble getting my sprite to appear on screen using my current method. I got multiple sprites on the screen already, but I'v added this new one in a different manner as I'm going to manipulate it later on (handling different interactions and such). But I don't see why it doesn't appear on the screen when I run the program. I'v tried changing the z order without luck, but that might be because the sprite wasn't added in the init method like the others so the z order doesn't affect it in comparison to the background and such created in the init method. Anyways, got any clue for why it won't draw on screen?

- (void) addMonster {

CCSprite * monster = [CCSprite spriteWithImageNamed:@"Ghost.png"];

// Determine where to spawn the monster along the Y axis
CGSize viewSize = [CCDirector sharedDirector].viewSize;
int minY = monster.contentSize.height / 2;
int maxY = viewSize.height - monster.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;

// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = ccp(viewSize.width + monster.contentSize.width/2, actualY);
[self addChild:monster];

// Determine speed of the monster
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Create the actions
CCActionMoveTo * actionMove = [CCActionMoveTo actionWithDuration:actualDuration
                                                        position:ccp(-monster.contentSize.width/2, actualY)];
CCActionCallBlock * actionMoveDone = [CCActionCallBlock actionWithBlock:^(CCNode *node) {
    [monster removeFromParentAndCleanup:YES];
}];
[monster runAction:[CCActionSequence actions:actionMove, actionMoveDone, nil]];   
}


//The call back function
-(void)gameLogic:(CCTime)dt {
[self addMonster];
}



- (id)init{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);


if( (self = [super init]) ) {

    self.userInteractionEnabled = YES;

//How often a new ghost gets produced
    [self schedule:@selector(gameLogic:) interval:1.0];

//And some more code none relevant to this keeps going...

1
with the code you have presented, make certain that the monster position type is CCPositionTypePoints (vis 'Normalized'). Also, when you set a position, it is relative to the object's parent. Make certain that the parent (self in your case) is dead-center on the screen.YvesLeBorg
I just realised that contentSize has the same size as viewSize as the image it self is as big as the screen, though the background is opaque and the ghost is very small, but the "whole" picture isn't. So had to fix the x and y positioning. Totally forgot before you told me to look at the CCPostionTypePoints, thx =)Sp3kk

1 Answers

0
votes

I am having the same problem.

The sprites i add do appear, but when i leave the scene and come back, they won't appear. As far as i know sprites needs to be added in the init method. When you add them at a later stage (in my case) they appear only once.

I am still looking for a solution to add the sprites at a later stage than the init method and appears more than once.

Maybe this answer helps you finding a solution.