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);
}
Touches for iPhone?
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Touches for iPhone?
The trunk code has an example of how to implement grabbing onto objects.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 2
- Joined: Sun Sep 06, 2009 11:09 pm
- Contact:
Re: Touches for iPhone?
I too am having a problem using touches on the iPhone, and I looked in the google code page, but I found nothing about touches. Care to be a little more specific as to where the example is?slembcke wrote:The trunk code has an example of how to implement grabbing onto objects.
-
- Posts: 40
- Joined: Fri Aug 28, 2009 8:36 am
- Contact:
Re: Touches for iPhone?
The function "click" is where the magic happens.
- Call cpSpacePointQueryFirst in order to intercept the object below the current mouse position
- Attach a pivot joint to the body
- When the user moves the pointer update joint position and velocity
- When the user release the button remove the joint
-
- Posts: 12
- Joined: Mon Jun 29, 2009 10:53 pm
- Contact:
Re: Touches for iPhone?
I used my own touch code at first, but then found the cpMouse.c/h files, and have been using it ever since, very easy to use, just add calls to your ccTouchesBegan/Moved/Ended/Cancelled methods. Just search for cpMouse in these forums. Or search for "grabbedtoo" iPhone project, that was an example I looked at to first see how to use cpMouse. There's a slight tweak to using cpMouse in these forums.
-RK
-RK
Who is online
Users browsing this forum: No registered users and 8 guests