开发者

cocos2d box2d scale sprite -> removechild (how?)

开发者 https://www.devze.com 2023-04-06 10:31 出处:网络
I am little slow in English, please do understand. Here is my source code: - (void)createBall:(CGPoint)touchedAt{

I am little slow in English, please do understand. Here is my source code:

- (void)createBall:(CGPoint)touchedAt{
    CGSize winSize = [CCDirector sharedDirector].winSize;
    ball2 = [CCSprite spriteWithFile:@"Ball.png" rect:CGRectMake(0, 0, 54, 54)];
    ball2.position = ccp(touchedAt.x,touchedAt.y);

    [self addChild:ball2];


    b2BodyDef ballBodyDef2;
    ballBodyDef2.type = b2_dynamicBody;
    ballBodyDef2.position.Set(touchedAt.x/PTM_RATIO, touchedAt.y/PTM_RATIO);
    ballBodyDef2.userData = ball2;
    b2Body *body2 = _world->CreateBody(&ballBodyDef2);
    b2CircleShape circle;
    circle.m_radius = 89.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO;
    b2FixtureDef ballShapeDef2;
    ballShapeDef2.shape = &circle;
    ballShapeDef2.density = 1.0f;
    ballShapeDef2.friction = 0.2f;
    ballShapeDef2.restitution = 0.8f;
    body2->CreateFixture(&ballShapeDef2);
}

-(void)createBall2
{

CGSize winSize = [CCDirector sharedDirector].winSize;
globalSprite                = [CCSprite spriteWithFile:@"Ball.png"];
globalSprite.position       = ccp(winSize.width/2 + globalSprite.contentSize.width, winSize.height/2);   
[self addChild:globalSprite];   

b2BodyDef ballBodyDef3;
ballBodyDef3.type = b2_dynamicBody;
ballBodyDef3.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
ballBodyDef3.userData = globalSprite  ;
b2Body *body3 = _world->CreateBody(&ballBodyDef3);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO;
b2FixtureDef ballShapeDef3;
ballShapeDef3.shape = &circle;
ballShapeDef3.density = 1.0f;
ballShapeDef3.friction = 0.2f;
ballShapeDef3.restitution = 0.8f;
body3->CreateFixture(&ballShapeDef3);
}

// initialize your instance here
-(id) init
{
if( (self=[super init])) {

    // enable touch

    // enable accelerometer
    CGSize winSize = [CCDirector sharedDirector].winSize;

    self.isAccelerometerEnabled = YES;
    self.isTouchEnabled = YES;

    // Create sprite and add it to the layer
// Create a world
    b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
    bool doSleep = true;
    _world = new b2World(gravity, doSleep);

    // Create edges around the entire screen
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0,0);
    b2Body *groundBody = _world->CreateBody(&groundBodyDef);
    b2PolygonShape 开发者_如何学CgroundBox;
    b2FixtureDef boxShapeDef;
    boxShapeDef.shape = &groundBox;
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);

    // Create ball body and shape



    [self schedule:@selector(tick:)];

    //[self schedule:@selector(gameLogic:) interval:1.0];

    [self createBall2];

}
return self;
}





- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

[self createBall:location];

}




- (void)tick:(ccTime) dt {    
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) { 

    if (b->GetUserData() != NULL) {
        CCSprite *ballData = (CCSprite *)b->GetUserData();
        ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
                                b->GetPosition().y * PTM_RATIO);
        ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
    }        
  }    
}

I want to

touch -> sprite create(circle) -> sprite scale -> sprite remove

but

- (void)tick:(ccTime) dt        <---------- this is simulator turn off!

I want to way


Try this :

world->DestroyBody(sprite);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号