0
\$\begingroup\$

I am in the process of making a game and I need an object to move only when the buttons are pressed. I have a method that begins the movement, and so far I am ending the movement of an object by having destroying the body of the object. The problem that I have is that every subsequent time that I try and move the ship the program crashes.

Here is my code.

Initializer method that contains the shipBody and its definition.

-(id) init
{
    if( (self=[super init])) {

        CGSize s = [CCDirector sharedDirector].winSize;


        //create spaceShip sprite and add it to the layer
        _ship = [CCSprite spriteWithFile:@"theShip.gif" ];
        _ship.position = ccp(s.width/2, 1.25*_ship.contentSize.height);
        [self addChild:_ship];

        //create the world
        b2Vec2 gravity = b2Vec2_zero;
        _world = new b2World(gravity);

        //create ship body
        b2BodyDef shipBodyDef;
        shipBodyDef.type = b2_dynamicBody;
        shipBodyDef.position.Set((s.width/2)/PTM_RATIO, (1.25*_ship.contentSize.height)/PTM_RATIO);
        shipBodyDef.userData = _ship;

        if(_shipBody == NULL){
        _shipBody =_world->CreateBody(&shipBodyDef);
        }

        //create ship shape
        b2PolygonShape shipShape;
        shipShape.SetAsBox(_ship.contentSize.width/PTM_RATIO/2, _ship.contentSize.height/PTM_RATIO/2);

        //create Ship definition and add to body
        b2FixtureDef ShipShapeDef;
        ShipShapeDef.shape = &shipShape;
        ShipShapeDef.density = 3;
        ShipShapeDef.friction =0;
        ShipShapeDef.restitution =0;
        _shipFixture = _shipBody->CreateFixture(&ShipShapeDef);

        ......
}

When the paddle is pressed and movement happens logic. THis is related to the problem since this is how movement of a sprite is achieved in the first place.

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Set up a way for touches to be turned into locations onscreen
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location ];

    //check to see if Left Paddle is being pressed
    if (CGRectContainsPoint([_paddle1 boundingBox], location)){
        b2Vec2 force = b2Vec2(-5,0);
        _shipBody->ApplyLinearImpulse(force, _shipBody ->GetPosition());
        }
    if (CGRectContainsPoint([_paddle2 boundingBox], location)){
        b2Vec2 force = b2Vec2(5,0);
        _shipBody->ApplyLinearImpulse(force, _shipBody->GetPosition());
    }
}

Logic for when touches stop. This is the problem area since destroying the shipBody stops all future movement as well.

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    _world->DestroyBody(_shipBody);
 }
 @end
\$\endgroup\$
  • \$\begingroup\$ Telling us what you want and showing us all your code does not constitute a question. I suggest you try this, then ask specific questions about the trouble you had with it. \$\endgroup\$ – MichaelHouse Mar 10 '14 at 20:50
  • \$\begingroup\$ How is the update? \$\endgroup\$ – user3084800 Mar 11 '14 at 6:44
  • \$\begingroup\$ @Byte56 looks like the question is solvable now. \$\endgroup\$ – Coyote Mar 11 '14 at 14:13
  • \$\begingroup\$ Please narrow the code down to just the problem area. \$\endgroup\$ – MichaelHouse Mar 11 '14 at 14:34
  • \$\begingroup\$ @Byte56 I have narrowed down the code and removed a lot of unnecessary pieces unrelated to the problem \$\endgroup\$ – user3084800 Mar 11 '14 at 17:56
0
\$\begingroup\$

I modified the ccTouchesBegan method to the following:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Set up a way for touches to be turned into locations onscreen
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location ];

    //check to see if Left Paddle is being pressed
    if (CGRectContainsPoint([_paddle1 boundingBox], location)){
        b2Vec2 force = b2Vec2(-5,0);
        _shipBody->SetLinearVelocity(force);
        }
    if (CGRectContainsPoint([_paddle2 boundingBox], location)){
        b2Vec2 force = b2Vec2(5,0);
        _shipBody->SetLinearVelocity(force);

    }
}

and the CCTouchesEnded to:

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    _shipBody->SetLinearVelocity(b2Vec2(0,0));



}

Using setLinear Velocity over applying linear impulse allows for the velocity to be changed if the buttons are no longer being pressed. Applying linear impulse can't be stopped, but only dampened which would require me to write additional code to create a dampening method in the ccTouchesEnded.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.