First, sorry for my english, it's not my native language...
I try to draw a bouncing menu with 20 buttons for an iPhone app.
I thought it will be easier to use MenuItem and to add the items/boxes to a shape. And, with a MenuItem, it was easy to have a selectedImage
My problem is my app is very slow...but I have just 20 boxes ! It's not very usable.
Is it because I use MenuItems ?
Code: Select all
-(id) init {
cpInitChipmunk();
staticBody = cpBodyNew(INFINITY, INFINITY);
space = cpSpaceNew();
cpSpaceResizeStaticHash(space, 400.0f, 40);
cpSpaceResizeActiveHash(space, 100, 600);
space->gravity = cpv(0, -500);
makeStaticBox(0, 110,320, 360);
space->elasticIterations=50;
MenuItem *img =[MenuItemImage itemFromNormalImage:@"img.png" selectedImage:@"imgON.png" target:self selector:@selector(myaction:)];
MenuItem *img2 =[MenuItemImage itemFromNormalImage:@"img2.png" selectedImage:@"imgON2.png" target:self selector:@selector(myaction2:)];
...
Menu *menu = [Menu menuWithItems: img, img2, ... nil];
[self addNewIconX:124 y:409 MenuItem:img1 Label:@"my button 1"];
[self addNewIconX:200 y:409 MenuItem:img2 Label:@"my button 2"];
....
[self schedule: @selector(step:)];
}
-(void) addNewIconX: (float)x y:(float)y MenuItem:(MenuItem*)appIcon Label:(NSString*)appName
{
int num = 4;
cpVect verts[] = {
cpv(-30,-37),
cpv(-30, 37),
cpv( 30, 37),
cpv( 30,-37),
};
cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero));
body->p = cpv(x, y);
cpSpaceAddBody(space, body);
cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero);
shape->e = 0.5f; shape->u = 0.5f;
shape->data = appIcon;
cpSpaceAddShape(space, shape);
}
-(void) step: (ccTime) delta {
int steps = 2;
cpFloat dt = delta/(cpFloat)steps;
for(int i=0; i<steps; i++){
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &eachShape, nil);
}
//main cocos2d rendering loop, this has to be streamlined
// e.g dont add logs here :/
static void eachShape(void *ptr, void* unused){
cpShape *shape = (cpShape*) ptr;
MenuItem *sprite = shape->data;
if( sprite ) {
cpBody *body = shape->body;
[sprite setPosition: cpv( body->p.x, body->p.y)];
[sprite setRotation: CC_RADIANS_TO_DEGREES( -body->a )];
}
}