patternsqlModerate
Sample game code for applying to a development firm
Viewed 0 times
samplefirmgamedevelopmentforcodeapplying
Problem
I am contemplating working with a development firm, and had asked for this piece of sample code. Could you please take a look and let me know if this is a quality piece of work, or if it needs improvement?
```
// textures pvr
-(void)startWaitAnimation
{
if(![self numberOfRunningActions] && self.visible)
{
[super startWaitAnimation];
[[CCTextureCache sharedTextureCache] addImageAsync:@"lion_wait2.pvr.ccz" target:self selector:@selector(_startWaitAnimation)];
}
}
-(void)_startWaitAnimation
{
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] textureForKey:@"lion_wait2.pvr.ccz"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"lion_wait2.plist" texture:texture];
NSMutableArray *waitFrames = [NSMutableArray array];
for(int i = 1; i 0)? 1 + (distance / 768) 2 : 1 + (-distance / 768) 2;
NSLog(@"mul = %f, distance %f ",mul, distance);
CGPoint pos1 = CGPointMake(rodHomePosition.x, rodHomePosition.y + distance/3 );
CCSequence action = [CCSequence actions:[CCMoveTo actionWithDuration:0.2mul position:pos1],[CCMoveTo actionWithDuration:0.1*mul position:rodHomePosition], nil];
[rod runAction:action];
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"worm.png"];
CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:texture rect:worm.textureRect];
[worm setDisplayFrame:frame];
//ccDrawPoint(wormHomePosition);
if(distanse > 240 )
{
[delegate changeLevel];
[[AudioEngine sharedEngine] playEffect:kUpSound];
}
}
//rotation with finger
-(void)ccTouchMoved:(UITouch )touch withEvent:(UIEvent )event
{
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoin
```
// textures pvr
-(void)startWaitAnimation
{
if(![self numberOfRunningActions] && self.visible)
{
[super startWaitAnimation];
[[CCTextureCache sharedTextureCache] addImageAsync:@"lion_wait2.pvr.ccz" target:self selector:@selector(_startWaitAnimation)];
}
}
-(void)_startWaitAnimation
{
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] textureForKey:@"lion_wait2.pvr.ccz"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"lion_wait2.plist" texture:texture];
NSMutableArray *waitFrames = [NSMutableArray array];
for(int i = 1; i 0)? 1 + (distance / 768) 2 : 1 + (-distance / 768) 2;
NSLog(@"mul = %f, distance %f ",mul, distance);
CGPoint pos1 = CGPointMake(rodHomePosition.x, rodHomePosition.y + distance/3 );
CCSequence action = [CCSequence actions:[CCMoveTo actionWithDuration:0.2mul position:pos1],[CCMoveTo actionWithDuration:0.1*mul position:rodHomePosition], nil];
[rod runAction:action];
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"worm.png"];
CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:texture rect:worm.textureRect];
[worm setDisplayFrame:frame];
//ccDrawPoint(wormHomePosition);
if(distanse > 240 )
{
[delegate changeLevel];
[[AudioEngine sharedEngine] playEffect:kUpSound];
}
}
//rotation with finger
-(void)ccTouchMoved:(UITouch )touch withEvent:(UIEvent )event
{
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoin
Solution
for(int i = 1; i <= 19; i++) {
i = ( i%2 == 0 && i!=19 ) ? i+1 : i;
[waitFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:[NSString stringWithFormat:@"lev_wait2%04d.png", i]]];
}Holy Toledo.
First of all, if you must update the iterator within a
for loop, let's leave the update statement empty:for (int i = 1; i <= 19; /*updated within loop*/) {As written, your loop misleadingly suggests we'll go from 1 to 19, run 19 times exactly, in order, and that's it. Then we sometimes increment in the loop...
But even that's not necessary. Honestly, all your ternary operator seems to be doing is ensuring that
i is an odd number. There's a much better way to do this:for (int i = 1; i <= 19; i += 2) {Now we're incrementing by two on each loop. The end result is identical (almost). The readability is a million times better. The ternary is completely removed.
(Technically, the end result is better because now we'll add 2 instead of adding 1 and then adding 1 again.)
Now, there's something else important to note. In the body of that loop, all this nesting just hampers the readability. Unnesting these method calls doesn't negatively impact performance.
So the final form of the loop should look something more like this:
for (int i = 1; i <= 19; i += 2) {
NSString *spriteName = [NSString stringWithFormat:@"lev_wait2%04d.png",i];
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[waitFrames addObject:cache spriteFrameByName:spriteName];
}Code Snippets
for(int i = 1; i <= 19; i++) {
i = ( i%2 == 0 && i!=19 ) ? i+1 : i;
[waitFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:[NSString stringWithFormat:@"lev_wait2%04d.png", i]]];
}for (int i = 1; i <= 19; /*updated within loop*/) {for (int i = 1; i <= 19; i += 2) {for (int i = 1; i <= 19; i += 2) {
NSString *spriteName = [NSString stringWithFormat:@"lev_wait2%04d.png",i];
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[waitFrames addObject:cache spriteFrameByName:spriteName];
}Context
StackExchange Code Review Q#10665, answer score: 15
Revisions (0)
No revisions yet.