Touches for iPhone?
Posted: Thu Aug 13, 2009 9:19 pm
Can anyone tell me how to enable touches for my objects.
Here is my code:
- (void)viewDidLoad {
background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background1.png"]];
background.center = CGPointMake(160,230);
floor = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"floor.png"]];
floor.center = CGPointMake(160, 350);
block = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2x1.png"]];
block.center = CGPointMake(160, 230);
block2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1x1.png"]];
block2.center = CGPointMake(160, 230);
circle1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1Circle.png"]];
circle1.center = CGPointMake(160, 230);
[self.view addSubview:background];
[self.view addSubview:Done3];
[self.view addSubview:floor];
[self.view addSubview:block];
[self.view addSubview:block2];
[self.view addSubview:circle1];
[self setupChipmuck];
}
-(void)setupChipmuck {
cpInitChipmunk();
space = cpSpaceNew();
space->gravity = cpv(0, -100);
space->elasticIterations = 10;
[NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
// Create our ball's body with 100 mass and infinite moment
cpBody *blockBody = cpBodyNew(100.0, INFINITY);
// Set the initial position
blockBody->p = cpv(60, 400);
// Add the body to the space
cpSpaceAddBody(space, blockBody);
// Create our shape associated with the ball's body
cpShape *blockShape = cpCircleShapeNew(blockBody, 38.0, cpvzero);
blockShape->e = 0.5; // Elasticity
blockShape->u = 1.0; // Friction
blockShape->data = block; // Associate with out ball's UIImageView
blockShape->collision_type = 1; // Collisions are grouped by types
// Add the shape to out space
cpSpaceAddShape(space, blockShape);
// Create our ball's body with 100 mass and infinite moment
cpBody *block2Body = cpBodyNew(100.0, INFINITY);
// Set the initial position
block2Body->p = cpv(200, 300);
// Add the body to the space
cpSpaceAddBody(space, block2Body);
cpShape *block2Shape = cpCircleShapeNew(block2Body, 38.0, cpvzero);
block2Shape->e = 1.2;
block2Shape->u = 0.3;
block2Shape->data = block2;
block2Shape->collision_type = 1;
cpSpaceAddShape(space, block2Shape);
//Circle
cpBody *circle1Body = cpBodyNew(100.0, INFINITY);
circle1Body->p = cpv(200, 225);
cpSpaceAddBody(space, circle1Body);
cpShape *circle1Shape = cpCircleShapeNew(circle1Body, 36.0, cpvzero);
circle1Shape->e = 2.0;
circle1Shape->u = 0.3;
circle1Shape->data = circle1;
circle1Shape->collision_type = 1;
cpSpaceAddShape(space, circle1Shape);
//Floor
cpBody *floorBody = cpBodyNew(INFINITY, INFINITY);
floorBody->p = cpv(160, 480-350);
cpVect verts1[] = { cpv(0.0, 0.0), cpv(50.0, 0.0), cpv(45.0, -15.0), cpv(0.0, -15.0) };
cpVect verts2[] = { cpv(50.0, 0.0), cpv(116.0, -66.0), cpv(110.0, -81.0), cpv(45.0, -15.0) };
cpVect verts3[] = { cpv(116.0, -66.0), cpv(204.0, -66.0), cpv(210.0, -81.0), cpv(110.0, -81.0) };
cpVect verts4[] = { cpv(204.0, -66.0), cpv(270.0, 0.0), cpv(275.0, -15.0), cpv(210.0, -81.0) };
cpVect verts5[] = { cpv(270.0, 0.0), cpv(320.0, 0.0), cpv(320.0, -15.0), cpv(275.0, -15.0) };
cpShape *floorShape = cpPolyShapeNew(floorBody, 4, verts1, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts2, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts3, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts4, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts5, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
}
Here is my code:
- (void)viewDidLoad {
background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background1.png"]];
background.center = CGPointMake(160,230);
floor = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"floor.png"]];
floor.center = CGPointMake(160, 350);
block = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2x1.png"]];
block.center = CGPointMake(160, 230);
block2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1x1.png"]];
block2.center = CGPointMake(160, 230);
circle1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1Circle.png"]];
circle1.center = CGPointMake(160, 230);
[self.view addSubview:background];
[self.view addSubview:Done3];
[self.view addSubview:floor];
[self.view addSubview:block];
[self.view addSubview:block2];
[self.view addSubview:circle1];
[self setupChipmuck];
}
-(void)setupChipmuck {
cpInitChipmunk();
space = cpSpaceNew();
space->gravity = cpv(0, -100);
space->elasticIterations = 10;
[NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
// Create our ball's body with 100 mass and infinite moment
cpBody *blockBody = cpBodyNew(100.0, INFINITY);
// Set the initial position
blockBody->p = cpv(60, 400);
// Add the body to the space
cpSpaceAddBody(space, blockBody);
// Create our shape associated with the ball's body
cpShape *blockShape = cpCircleShapeNew(blockBody, 38.0, cpvzero);
blockShape->e = 0.5; // Elasticity
blockShape->u = 1.0; // Friction
blockShape->data = block; // Associate with out ball's UIImageView
blockShape->collision_type = 1; // Collisions are grouped by types
// Add the shape to out space
cpSpaceAddShape(space, blockShape);
// Create our ball's body with 100 mass and infinite moment
cpBody *block2Body = cpBodyNew(100.0, INFINITY);
// Set the initial position
block2Body->p = cpv(200, 300);
// Add the body to the space
cpSpaceAddBody(space, block2Body);
cpShape *block2Shape = cpCircleShapeNew(block2Body, 38.0, cpvzero);
block2Shape->e = 1.2;
block2Shape->u = 0.3;
block2Shape->data = block2;
block2Shape->collision_type = 1;
cpSpaceAddShape(space, block2Shape);
//Circle
cpBody *circle1Body = cpBodyNew(100.0, INFINITY);
circle1Body->p = cpv(200, 225);
cpSpaceAddBody(space, circle1Body);
cpShape *circle1Shape = cpCircleShapeNew(circle1Body, 36.0, cpvzero);
circle1Shape->e = 2.0;
circle1Shape->u = 0.3;
circle1Shape->data = circle1;
circle1Shape->collision_type = 1;
cpSpaceAddShape(space, circle1Shape);
//Floor
cpBody *floorBody = cpBodyNew(INFINITY, INFINITY);
floorBody->p = cpv(160, 480-350);
cpVect verts1[] = { cpv(0.0, 0.0), cpv(50.0, 0.0), cpv(45.0, -15.0), cpv(0.0, -15.0) };
cpVect verts2[] = { cpv(50.0, 0.0), cpv(116.0, -66.0), cpv(110.0, -81.0), cpv(45.0, -15.0) };
cpVect verts3[] = { cpv(116.0, -66.0), cpv(204.0, -66.0), cpv(210.0, -81.0), cpv(110.0, -81.0) };
cpVect verts4[] = { cpv(204.0, -66.0), cpv(270.0, 0.0), cpv(275.0, -15.0), cpv(210.0, -81.0) };
cpVect verts5[] = { cpv(270.0, 0.0), cpv(320.0, 0.0), cpv(320.0, -15.0), cpv(275.0, -15.0) };
cpShape *floorShape = cpPolyShapeNew(floorBody, 4, verts1, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts2, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts3, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts4, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
floorShape = cpPolyShapeNew(floorBody, 4, verts5, cpv(-320.0f / 2, 81.0f / 2));
floorShape->e = 0.5; floorShape->u = 0.5; floorShape->collision_type = 0;
floorShape->data = floor;
cpSpaceAddStaticShape(space, floorShape);
}