Wednesday, March 10, 2010

Animate sprite

sheet99 = [CCSpriteSheet spriteSheetWithFile:@"fan_l1l2_left.png" capacity:12];
[self addChild:sheet99 z:4 tag:kTagSpriteSheet];
sprite99 = [sheet99 createSpriteWithRect:CGRectMake(0,0,300,400)];//32 * idx,32 * idy,32,32
sprite99.position=ccp(4,14);
[sheet99 addChild:sprite99 z:7];




CCAnimation *animation = [CCAnimation animationWithName:@"dance" delay:.1f ];

[animation addFrameWithTexture:sheet99.texture rect:CGRectMake(-121,0,242,400)];//-121
[animation addFrameWithTexture:sheet99.texture rect:CGRectMake(0, 0, 242,400)];//0,0
id action = [CCAnimate actionWithAnimation:animation];
// Run the animation
//id repeatAction = [CCRepeat actionWithAction:action times:100];

// To repeat forever, use this
id repeatAction = [CCRepeatForever actionWithAction:action];

[sprite99 runAction:repeatAction];

body and updation

b2BodyDef bodyDef9c8;
bodyDef9c8.type =b2_staticBody;

bodyDef9c8.position.Set(9,9);//
//bodyDef9c8.angle=-58;
//bodyDef.userData = sprite;
b2Body* body9c8;
//= world->CreateBody(&groundBodyDef5);
body9c8 = world->CreateBody(&bodyDef9c8);

// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox9c8;
dynamicBox9c8.SetAsBox(1.7f, .1f);//These are mid points for our 1m box

// Define the dynamic body fixture.
b2FixtureDef fixture9c8;
//fixture99.userData = @"Box";

fixture9c8.shape = &dynamicBox9c8;
fixture9c8.density = 0;
fixture9c8.friction = 0.3f;
fixture9c8.restitution=1.1f;
body9c8->CreateFixture(&fixture9c8);



#####################
update
###################
body0->SetTransform (b2Vec2(8,12),0);//target replaced by setXform

Friday, February 19, 2010

Tuesday, February 9, 2010

stack in objc

for (float32 x =0.0f; x<300.0f; x=x+1.0f) {
float32 y=1.0f;
// Define the static body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;

bodyDef.position.Set(x, y);
b2Body *body = world->CreateBody(&bodyDef);

// Define another box shape for our static body.
b2PolygonShape staticBox;
staticBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box

// Define the static body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &staticBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}

Thursday, January 7, 2010

create body IPhone

// body
b2BodyDef groundBodyDef2;
groundBodyDef2.position.Set(8, 9);
groundBodyDef2.angle=28;
//groundBox2.restitution=1;
b2Body* groundBody2 = world->CreateBody(&groundBodyDef2);
b2PolygonShape groundBox2;
groundBox2.SetAsBox(1.5f, .1f);//SetAsEdge(b2Vec2(3,3), b2Vec2(3,0));
groundBody2->CreateFixture(&groundBox2);


// triangle
b2BodyDef polyBodyDef; // Define the body
polyBodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *poly = world->CreateBody(&polyBodyDef);

b2PolygonShape polyShapeDef; // Define the shape
//polyShapeDef.SetAsBox(1.0f, 1.0f);
polyShapeDef.m_vertexCount = 3;
polyShapeDef.m_vertices[0].Set(-1, 0);
polyShapeDef.m_vertices[1].Set(1, 0);
polyShapeDef.m_vertices[2].Set(0, 1.5);

b2FixtureDef polyFixtureDef; // Define the fixture
polyFixtureDef.shape = &polyShapeDef;
polyFixtureDef.density = 1.0f;
polyFixtureDef.friction = 0.3f;

poly->CreateFixture(&polyFixtureDef);

Thursday, December 17, 2009

Enable and disable keyboard

1) go into interface builder and add a text field.
2) Add this code in your .h file:

Code:

-(IBAction) endText;

3) Add this code in your .m file:

Code:

- (IBAction) endText {

}

Wednesday, December 16, 2009

Global variable in Obj c

It is simple. In any .h file, write:
Code:

extern int variable;

Put this declaration *outside* of any Objective-C block like @interface...@end. You can place it before the @interface or after the @end. Either way is just fine.

Then, define the variable in any .m file (it doesn't matter which one, but only place it in one file):

Code:

int variable;

Tuesday, December 15, 2009

playing sound infinitie and then stop on click

in .h add
#import AudioToolbox/AudioServices.h
#import AVFoundation/AVFoundation.h


and declare
AVAudioPlayer *audioPlayer

in .m

inside 1st loaded ()

NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"2000hz" ofType:@"wav"];

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];

[audioPlayer setDelegate:self];
[audioPlayer setNumberOfLoops:-1];
[audioPlayer prepareToPlay];
BOOL plays = [audioPlayer play];



and to stop sound
write this on view change


[audioPlayer stop];

Sunday, December 13, 2009

random Things under control

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
for(newBall in enemies)
{
newBall.center = CGPointMake(location.x,location.y);
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
for(newBall in enemies)
{
newBall.center = CGPointMake(location.x,location.y);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
for(newBall in enemies)
{
newBall.center = CGPointMake(location.x,location.y);
}
}

////////////

sound code

NSString * path = [[NSBundle mainBundle] pathForResource:@"ballhitbar" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);


and to play wrte this

AudioServicesPlaySystemSound(soundID);

///////////////////////////////////// random
//Here is the spawnE...

-(void)spawnE {



enemies = [[NSMutableArray alloc] init];
UIImage *enemyImage = [UIImage imageNamed:@"enemy.png"];
UIImageView *newEnemy = [[UIImageView alloc] initWithImage: enemyImage];



int randomX = arc4random() % 320; // number from 0 to 319

newEnemy.center = CGPointMake( randomX, 0);


[enemies addObject:newEnemy];


[self addSubview: newEnemy];





}


///////// or
enemies = [[NSMutableArray alloc] init];

UIImage *enemyImage = [UIImage imageNamed:@"bug.png"];


for (int i=0; i<12; i++){
newEnemy = [[UIImageView alloc] initWithImage: enemyImage];



int randomX = arc4random() % 320; // number from 0 to 319
int randomY = arc4random() % 480;

newEnemy.center = CGPointMake( randomX,randomY);// working all over screen



[enemies addObject:newEnemy];


[self addSubview: newEnemy];

//newEnemy.userInteractionEnabled = YES;

}
//[self move];



}



//////////////

Thursday, December 3, 2009

function sytanx in obj c

in .h file
//
-(void)create;


/// in .m file

-(void) create
{
// write here
}



// call function
[self create]; // either in viewDidload() or anywer

FEEDJIT Live