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

Monday, October 26, 2009

stack

for (i = 0; i < 50; i++) {
circleDef.radius = Math.random()/20+0.02;

bodyDef = new b2BodyDef();
bodyDef.position.Set(Math.random()*8+140,1);
bodyDef.allowSleep = true;
bodyDef.linearDamping = 0.1;
bodyDef.angularDamping = 0.1;

b = world.CreateBody(bodyDef);

b.CreateShape(circleDef);
b.SetMassFromShapes();


var circle:Circle=new Circle()
circle.x= b.GetPosition().x* 50
circle.y= b.GetPosition().y* 50
circle.rotation = b.GetAngle() * (180/Math.PI);

screen.addChild(circle)
b.m_userData = circle;
}

Thursday, September 24, 2009

Instantiation

Plus signs denote class methods, minus signs denote instance methods.
Class methods have no access to instance variables.
@implementation classname
+classMethod {
// implementation
}
-instanceMethod {
// implementation
}
@end


/////

Instantiation

Once an Objective-C class is written, it can be instantiated. This is done by first allocating the memory for a new object and then by initializing it. An object isn't fully functional until both steps have been completed. These steps are typically accomplished with a single line of code:

MyObject * o = [[MyObject alloc] init];

The alloc call allocates enough memory to hold all the instance variables for an object, and the init call can be overridden to set instance variables to specific values on creation. The init method is often written as follows:

-(id) init {
self = [super init];
if (self) {
ivar1 = value1;
ivar2 = value2;
.
.
.
}
return self;
}

////

Properties are implemented by way of the @synthesize keyword, which generates getter and setter methods according to the property declaration. Alternately, the @dynamic keyword can be used to indicate that accessor methods will be provided by other means.

Tuesday, September 22, 2009

Cocos2d

In Cocos2d iPhone you'll be dealing with the Scene and Layer classes frequently. A Scene is what the players can see at a given time, and is composed of one or more Layers. To display a specific Scene, you tell the Director (which is a singleton) to play it.

Friday, September 18, 2009

FEEDJIT Live