I'm new to cocos2d. I'm writing my first app. But I have a problem I can't solve it. The problem is that when I move multiple sprites, the others are OK except the last one. I want my sprites will run actions at the same time. But I don't know how to fix it. Any ideas or advice about promoting my code. Thanks.
Here's my code.
-(void) setHeadFace:(Face *)headFace moveFrom:(CGPoint)startPosition moveTo:(CGPoint)endPosition`
{
CCMoveTo *moveOut = [CCMoveTo actionWithDuration:0.2f position:startPosition];
CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.2f scale:0.0f];
CCSpawn *moveFrom = [CCSpawn actions:moveOut,scaleTo,nil];
CCMoveTo *moveTo = [CCMoveTo actionWithDuration:0.0f position:endPosition];
CCSequence *headAction = [CCSequence actions:moveFrom,moveTo,nil];
[headFace.faceSprite runAction:headAction];
}
-(void) setMidlFace:(Face *)curtFace moveTo:(CGPoint)movePosition nextFace:(Face *)nextFace
{
CCMoveTo *moveTo = [CCMoveTo actionWithDuration:0.2f position:movePosition];
CCSequence *moveAction = [CCSequence actions:moveTo,nil];
[curtFace.faceSprite runAction:moveAction];
nextFace.faceSprite = curtFace.faceSprite;
nextFace.faceType = curtFace.faceType;
[nextFace.faceSprite setPosition:movePosition];
}
-(void) setTailFace:(Face *)tailFace moveTo:(CGPoint)movePosition
byGuardSprite:(CCSprite *)guardSprite`
{
CCDelayTime *delay = [CCDelayTime actionWithDuration:0.2f];
CCMoveTo *moveIn = [CCMoveTo actionWithDuration:0.2f position:movePosition];
CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.2f scale:1.0f];
CCSpawn *moveTo = [CCSpawn actions:moveIn,scaleTo,nil];
CCCallBlock *touchBlock =[CCCallBlock actionWithBlock:^{
tailFace.faceSprite = guardSprite;
tailFace.faceType = guardSprite.tag;
tailFace.faceSprite.tag = -1;
isTouch = YES;
}];
CCSequence *tailAction=[CCSequence actions:delay,moveTo,touchBlock,nil];
[guardSprite runAction:tailAction];
}
-(void) moveFaces:(Face *)face direction:(int)direction`
{
CCSprite *guardSprite = nil;
CGPoint movePosition;
CGPoint curtPosition;
CGPoint startPosition;
CGPoint endPosition;
int x = face.cdX;
int y = face.cdY;
CGFloat width = faceGrid[x][0].position.x;
CGFloat height = faceGrid[0][y].position.y;
switch (direction) {
case MOVE_DOWN:
CCLOG(#"move down:direction %d", direction);
movePosition = CGPointMake(width,faceGrid[x][0].position.y);
//move the first face out of the grid
startPosition = CGPointMake(width,faceGrid[x][0].position.y-GRID_OFFSET.y/2);
endPosition = CGPointMake(width,faceGrid[x][GRID_HEIGHT-1].position.y+GRID_OFFSET.y/2);
[self setHeadFace:faceGrid[x][0] moveFrom:startPosition moveTo:endPosition];
guardSprite = faceGrid[x][0].faceSprite;
guardSprite.tag = faceGrid[x][0].faceType;
for (int j=1; j<GRID_HEIGHT; j++)
{
curtPosition=CGPointMake(width,faceGrid[x][j].position.y);
//move the middle face to the next face
[self setMidlFace:faceGrid[x][j] moveTo:movePosition nextFace:faceGrid[x][j-1]];
movePosition = curtPosition;
}
//use the guard face to set the last face's move action
[self setTailFace:faceGrid[x][GRID_HEIGHT-1] moveTo:movePosition byGuardSprite:guardSprite];
break;
......
}
I used tick function for same purpose.
-(void)onEnter
{
[super onEnter];
self.isTouchEnabled = true;
[self schedule: #selector(tick:)];
}
-(void) tick: (ccTime) dt
{
CGPoint pos = head.position ;
head.position = ccp(pos.x+gameSpeed, pos.y);
pos = tail.position ;
tail.position = ccp(pos.x+gameSpeed, pos.y);
}
Related
Need some help as a beginner: I've got different nodes: 4 squares (sprite1) and 1 counter (counterLabel, counts the nodes, which have been removed). I want to remove the 4 squares by touching them. With the code below the squares can be removed, but also the counter. Strangely enough, because I tried to address the square nodes (sprite1) exclusively. Is there any possibility to remove the square nodes (sprite 1) exclusively?
#implementation GameScene {
BOOL updateLabel;
SKLabelNode *counterLabel;
}
int x;
int y;
int counter;
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]){
self.backgroundColor = [SKColor /*colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0*/ whiteColor];
counter = 0;
updateLabel = false;
counterLabel = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
counterLabel.name = #"myCounterLabel";
counterLabel.text = #"0";
counterLabel.fontSize = 48;
counterLabel.fontColor = [SKColor greenColor];
//counterLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
//counterLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBottom;
counterLabel.position = CGPointMake(50,50); // change x,y to location you want
//counterLabel.zPosition = 900;
[self addChild: counterLabel];
}
return self;
}
-(void) didMoveToView:(SKView *)view {
SKTexture *texture1 = [SKTexture textureWithImageNamed:#"square"];
for (int i = 0; i < 4; i++) {
x = arc4random()%668;
y = arc4random()%924;
SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithTexture:texture1];
sprite1.position = CGPointMake(x, y);
sprite1.name = #"square";
[self addChild:sprite1];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSArray *nodes = [self nodesAtPoint: [touch locationInNode: self]];
for (SKNode *sprite1 in nodes) {
[sprite1 removeFromParent];
counter ++;
updateLabel = true;
}
}
-(void)update:(CFTimeInterval)currentTime {
if(updateLabel == true){
counterLabel.text = [NSString stringWithFormat:#"%i",counter];
updateLabel = false;
}
}
#end
you should use the property name of SKSpriteNode
in this case you can do:
for (SKNode *sprite1 in nodes) {
if(![sprite1.name isEqualToString:#"myCounterLabel"]) {
[sprite1 removeFromParent];
}
counter ++;
updateLabel = true;
}
So if the SKNode name is different to the name of counterLabel, then removeFromParent.
I am new in cocos2d. I have a problem when we create new sprite object. It's not remove onto the display. Sprite object are not delete when we add new lives.(add heart sprite).
//Here I create a live which in heart shape.
-(id) init {
if( (self=[super init]) ) {
hearthArray = [[NSMutableArray alloc] init];
lives = 4;
for(NSInteger ilive = 0; ilive<lives; ilive++){
CCSprite *hearth = [CCSprite spriteWithFile:#"hearth.png"];
hearth.position = ccp( ((ilive+1)*50), winSize.height - 50);
[hearthArray insertObject:hearth atIndex:ilive];
[self addChild:hearth];
}
return self;
}
//Below code into remove the heart.(decrease lives).
- (void) addMonster:(ccTime)dt {
//select a random monster from the _monsters Array
int selectedMonster = arc4random() % [_monsters count];
Monster *monster = [_monsters objectAtIndex:selectedMonster];
int m = [monster movement];
CCSprite *spriteMonster = [[CCSprite alloc] initWithFile:[monster monsterSprite]];
spriteMonster.tag = [monster tag];
CGSize winSize = [CCDirector sharedDirector].winSize;
int minX = spriteMonster.contentSize.width / 2;
int maxX = winSize.width - spriteMonster.contentSize.width/2;
int rangeX = maxX - minX;
int actualY = (arc4random() % rangeX) + minX;
//BLOCK 2 - Determine speed of the monster
int minDuration = [monster minVelocity];
int maxDuration = [monster maxVelocity];
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
if(m == 1){
spriteMonster.position = ccp( actualY,winSize.height + spriteMonster.contentSize.height/2);
[self addChild:spriteMonster];
//BLOCK 4 - Create the actions
CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp( actualY,-spriteMonster.contentSize.height/2)];
CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
[_monstersOnScreen removeObject:node];
[node removeFromParentAndCleanup:YES];
// Remove lifes
lives--;
// [[hearthArray lastObject] removeFromParentAndCleanup:YES];
[self removeChild:[hearthArray lastObject] cleanup:YES];
[hearthArray removeLastObject];
NSLog(#"m=1 when array : %#",hearthArray);
if(lives == 0)
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
}];
[spriteMonster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
[_monstersOnScreen addObject:spriteMonster];
}
}
//Below into Add new lives using for loop.when touch particular object.
-(void)increaseLivesWhentouchCoin{
NSLog(#"lives is get when add live : %i",lives);
NSLog(#"hearthArray when toch coin: %#",hearthArray);
lives = lives+1;
NSLog(#"lives+1 : %i",lives);
for(NSInteger i = 0; i<lives; i++){
hearth = [CCSprite spriteWithFile:#"hearth.png"];
CGSize winSize = [CCDirector sharedDirector].winSize;
hearth.position = ccp( ((i+1)*50), winSize.height-50);
[hearthArray insertObject:hearth atIndex:i];
[self addChild:hearth];
}
NSLog(#"hearthArray out for loop: %#",hearthArray);
}
Please help me.Thanks in advance.
Your increaseliveswhentouch coin method should be like this..
-(void)increaseLivesWhentouchCoin{
CCSprite *hearth = [CCSprite spriteWithFile:#"hearth.png"];
CGSize winSize = [CCDirector sharedDirector].winSize;
hearth.position = ccp( ((lives+1)*50), winSize.height-50);
[hearthArray insertObject:hearth atIndex:lives];
[self addChild:hearth];
lives++;
}
You have to add just one heart object .There is no need to create all objects again,if you want to create remove all the objects first..
I am new to cocos2d so please help me if u can
I have background moving from right to left, and the background contains small windows with 3 rows of windows
_spaceDust1 = [CCSprite spriteWithFile:#"bg_front_spacedust.png"];
_spaceDust2 = [CCSprite spriteWithFile:#"bg_front_spacedust.png"];
CGPoint dustSpeed = ccp(0.1 , 0.1);
CGPoint bgSpeed = ccp(0.05 , 0.05);
[_backgroundNode addChild:_spaceDust1 z:0 parallaxRatio:dustSpeed positionOffset:ccp(0,winSize.height / 2)];
[_backgroundNode addChild:_spaceDust2 z:0 parallaxRatio:dustSpeed positionOffset:ccp(_spaceDust1.contentSize.width , winSize.height / 2)];
Now add enemies wich also move from right to left with same speed
_robbers = [[CCArray alloc] initWithCapacity:kNumAstroids];
for (int i = 0; i < kNumAstroids; ++i) {
CCSprite *asteroid = [CCSprite spriteWithSpriteFrameName:#"robber.png"];
asteroid.visible = NO;
[_batchNode addChild:asteroid];
[_robbers addObject:asteroid];
}
in update method:
double curTime = CACurrentMediaTime();
if (curTime > _nextRunemanSpawn) {
float randSecs = [self randomValueBetween:0.20 andValue:1.0];
_nextRunemanSpawn = randSecs + curTime;
float randY = 80.0;
float randY1 = 185.0;
float randY2 = 293.0;
float randDuration = [self randomValueBetween:5.2 andValue:5.2];
float randDuration1 = [self randomValueBetween:1.0 andValue:1.0];
CCSprite *asteroid = [_robbers objectAtIndex:_nextRobber];
_nextRobber++;
if (_nextRobber >= _robbers.count) {
_nextRobber = 0;
}
//[asteroid stopAllActions];
int winChoice = arc4random() % 3;
if (winChoice == 0) {
asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY);
asteroid.visible = YES;
}
else if(winChoice == 1){
asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY1);
asteroid.visible = YES;
}else {
asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY2);
asteroid.visible = YES;
}
[asteroid runAction:[CCSequence actions:[CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width-asteroid.contentSize.width, 0)],
[CCCallFuncN actionWithTarget:self selector:#selector(setInvisible:)],nil]];
All is going well but i want to set this enemies in to window and in random position
so how can i set x-argument of enemies so it can be fix in to window of the background?
For some reason I cannot comment so this is written as an answer. What exactly are you trying to do? I am a bit confused. It sounds like you want the X position to be set so that it is in one of 3 random positions, is that correct?
I have created a HudLayer class which defines a protocol for when button presses happen.
-(id) init
{
if( (self=[super initWithColor:ccc4(255, 255, 0, 128)])) {
self.position=ccp(0,0);
self.contentSize=CGSizeMake(480, 40);
scoreLabel=[CCLabelTTF labelWithString:#"0" fontName:#"Marker Felt" fontSize:24];
scoreLabel.position=ccp(62,20);
scoreLabel.anchorPoint=ccp(0,0.5);
[self addChild:scoreLabel];
CCLabelTTF *textLabel=[CCLabelTTF labelWithString:#"Score:" fontName:#"Marker Felt" fontSize:24];
textLabel.position=ccp(5,20);
textLabel.anchorPoint=ccp(0,0.5);
[self addChild:textLabel];
CCMenuItemImage * item = [CCMenuItemImage itemWithNormalImage:#"fire.png" selectedImage:#"fire.png" target:self selector:#selector(projectileButtonTapped:)];
CCMenu *menu = [CCMenu menuWithItems:item, nil];
menu.position = ccp(150, 20);
[self addChild:menu];
}
return self;
}
- (void)projectileButtonTapped:(id)sender
{
NSLog(#"projectileButtonTapped HudLayer");
[self.delegate respondsToSelector:#selector(projectileButtonTapped:)];
}
Then in my HelloWorldLayer class I implement the protocol and set myself as the delegate.
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
//add another layer to the scene
// HudLayer *anotherLayer = [HudLayer node];
// anotherLayer.delegate = self;
// [scene addChild: anotherLayer];
// layer.hud=anotherLayer;
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
_tileMap = [CCTMXTiledMap tiledMapWithTMXFile:#"GridWars#medium.tmx"];
_background = [_tileMap layerNamed:#"Background"];
_foreground = [_tileMap layerNamed:#"Foreground"];
_meta = [_tileMap layerNamed:#"Meta"];
_meta.visible = NO;
CCTMXObjectGroup *objectGroup = [_tileMap objectGroupNamed:#"Objects"];
NSAssert(objectGroup != nil, #"tile map has no objects object layer");
NSDictionary *spawnPoint = [objectGroup objectNamed:#"SpawnPoint"];
int x = [spawnPoint[#"x"] integerValue];
int y = [spawnPoint[#"y"] integerValue];
_wizardHero = [[WizardHero alloc]init];
_redEnemy = [[RedEnemy alloc]init];
_wizardHero.position = ccp(x,y);
[self addChild:_wizardHero];
[self setViewPointCenter:_wizardHero.position];
[self addChild:_tileMap z:-1];
self.touchEnabled = YES;
self.enemies = [[NSMutableArray alloc] init];
self.projectiles = [[NSMutableArray alloc] init];
[self schedule:#selector(testCollisions:)];
for (spawnPoint in [objectGroup objects]) {
if ([[spawnPoint valueForKey:#"Enemy"] intValue] == 1){
x = [[spawnPoint valueForKey:#"x"] intValue];
y = [[spawnPoint valueForKey:#"y"] intValue];
[self addEnemyAtX:x y:y];
}
}
_hud = [HudLayer node];
_hud.delegate = self;
[self addChild:_hud];
}
return self;
}
- (void)projectileButtonTapped:(id)sender
{
// Find where the touch is
// CGPoint touchLocation = [touch locationInView: [touch view]];
// touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
// touchLocation = [self convertToNodeSpace:touchLocation];
if (self.wizardHero.selectedTargets.count > 0) {
// Create a projectile and put it at the player's location
CCSprite *projectile = [CCSprite spriteWithFile:#"Projectile.png"];
projectile.position = _wizardHero.position;
[self addChild:projectile];
// Determine where we wish to shoot the projectile to
int realX;
// Are we shooting to the left or right?
CGPoint diff = ccpSub(self.redEnemy.position, self.wizardHero.position);
if (diff.x > 0)
{
realX = (_tileMap.mapSize.width * _tileMap.tileSize.width) +
(projectile.contentSize.width/2);
} else {
realX = -(_tileMap.mapSize.width * _tileMap.tileSize.width) -
(projectile.contentSize.width/2);
}
float ratio = (float) diff.y / (float) diff.x;
int realY = ((realX - projectile.position.x) * ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);
// Determine the length of how far we're shooting
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:#selector(projectileMoveFinished:)];
[projectile runAction:
[CCSequence actionOne:
[CCMoveTo actionWithDuration: realMoveDuration
position: realDest]
two: actionMoveDone]];
[self.projectiles addObject:projectile];
}
}
The problem is that only the projectileButtonTapped: in the HudLayer is called (ie. the delegate's method never gets called).
P.S.
To save space I left out both .h files. But I assure you they are correct, with the #protocol used in HudLayer and that same protocol name put in <> within the HelloWorldLayer.
I think :
- (void)projectileButtonTapped:(id)sender
{
NSLog(#"projectileButtonTapped HudLayer");
if ([self.delegate respondsToSelector:#selector(projectileButtonTapped:)]) {
[self.delegate performSelector:#selector(projectileButtonTapped:) withObject:sender];
}
}
I have to move background images in iOS Coco2d but I am having a few difficulties. I have tried some solutions provided on some websites but have not been successful in getting them to work properly. Below is the code I am currently working on:-
The background moves smoothly the first time but it is not working properly after that:-
Code in init function :-
bg1 = [CCSprite spriteWithFile: #"bg1.png"];
bg1.anchorPoint = CGPointZero;
[self addChild:bg1 z:-2];
bg2 = [CCSprite spriteWithFile: #"bg1.png"];
[self addChild:bg2 z:-3];
bg2.anchorPoint = CGPointMake(480, 0);
// schedule a repeating callback on every frame
[self schedule:#selector(nextFrame:) interval:.4f];
- (void) nextFrame:(ccTime)dt {
id actionMove = [CCMoveTo actionWithDuration:.4 position:ccp(bg1.position.x - 100 * dt, bg1.position.y)]; //winSize.height/2)];
id actionMove1 = [CCMoveTo actionWithDuration:.4 position:ccp(bg2.position.x - 100 * dt, bg2.position.y)]; //winSize.height/2)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)];
[bg1 runAction:[CCSequence actions:actionMove,actionMoveDone, nil]];
[bg2 runAction:[CCSequence actions:actionMove1,actionMoveDone, nil]];
}
-(void)spriteMoveFinished:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
if(sprite == bg1) {
if (bg1.position.x < -480) {
[self removeChild:bg1 cleanup:NO];
bg1.position = ccp( 480 , bg1.position.y );
[self addChild:bg1 z:-2];
}
}
else if(sprite == bg2)
if (bg2.position.x < -480) {
[self removeChild:bg2 cleanup:NO];
bg2.position = ccp( bg1.position.x+ 480 , bg1.position.y );
[self addChild:bg2 z:-3];
}
}
}
Try this, make sure you flip background 2.
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define MM_BG_SPEED_DUR ( IS_IPAD ? (6.0f) : (2.0f) )
-(void)onEnter
{
[super onEnter];
[self initBackground];
[self schedule: #selector(tick:)];
}
-(void)initBackground
{
NSString *tex = #"BG/Background.png";//[self getThemeBG];
mBG1 = [CCSprite spriteWithFile:tex];
mBG1.position = ccp(s.width*0.5f,s.height*0.5f);
[self addChild:mBG1 z:LAYER_BACKGROUND];
mBG2 = [CCSprite spriteWithFile:tex];
mBG2.position = ccp(s.width+s.width*0.5f,s.height*0.5f);
mBG2.flipX = true;
[self addChild:mBG2 z:LAYER_BACKGROUND];
}
-(void)scrollBackground:(ccTime)dt
{
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint pos1 = mBG1.position;
CGPoint pos2 = mBG2.position;
pos1.x -= MM_BG_SPEED_DUR;
pos2.x -= MM_BG_SPEED_DUR;
if(pos1.x <=-(s.width*0.5f) )
{
pos1.x = pos2.x + s.width;
}
if(pos2.x <=-(s.width*0.5f) )
{
pos2.x = pos1.x + s.width;
}
mBG1.position = pos1;
mBG2.position = pos2;
}
-(void)tick:(ccTime)dt
{
[self scrollBackground:dt];
}
I think this way is a little simpler. You initialize the backgrounds in initand move them in update.
In the init method:
// position backgrounds
CCSprite *bg1 = [CCSprite spriteWithSpriteFrame:spriteFrame];
CCSprite *bg2 = [CCSprite spriteWithSpriteFrame:spriteFrame];
CCSprite *bg3 = [CCSprite spriteWithSpriteFrame:spriteFrame];
bg1.anchorPoint = ccp(0, 0);
bg1.position = ccp(0, 0);
bg2.anchorPoint = ccp(0, 0);
bg2.position = ccp(bg1.contentSize.width-1, 0);
bg3.anchorPoint = ccp(0, 0);
bg3.position = ccp(2*bg1.contentSize.width-1, 0);
_backgrounds = #[bg1, bg2, bg3];
[self addChild:bg1 z:INT_MIN];
[self addChild:bg2 z:INT_MIN];
[self addChild:bg3 z:INT_MIN];
In the update method:
// endless scrolling for backgrounds
for (CCSprite *bg in _backgrounds) {
bg.position = ccp(bg.position.x - 50 * delta, bg.position.y);
if (bg.position.x < -1 * (bg.contentSize.width)) {
bg.position = ccp(bg.position.x + (bg.contentSize.width*2)-2, 0);
}
}
Note: the code is for Cocos2d 3.0