Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Zombie Game

ad1269ad1269 Posts: 48New Users @
edited January 2013 in iOS SDK Development
Hi everyone, I have some code that detects collisions and fires a message to the update method when the player hits a wall. This method is the one that animates the player. If it gets the message that a collision has been detected, then I need it to stop moving the player into the wall. This way the player cannot just pass through walls and escape zombies.

-(void)update:(int) collide {

CCSprite *controlledObject = (CCSprite *)_controlledObject;

CGPoint cPoint = controlledObject.position;

CGFloat xMinLimit = [self getXMinimumLimit];
CGFloat xMaxLimit = [self getXMaximumLimit];

CGFloat yMinLimit = [self getYMinimumLimit];
CGFloat yMaxLimit = [self getYMaximumLimit];

//N = point * 1 / _joystickRadius
CGFloat xSpeedRatio = _controllerActualPoint.x / _joystickRadius;
CGFloat ySpeedRatio = _controllerActualPoint.y / _joystickRadius;

if (collide == 1) {
NSLog(@" COLLISION DETECTED");

// Code for stopping the player goes here.

}

else {

//BORDERS
//X limits
if (cPoint.x < xMinLimit) {
if (xSpeedRatio < 0) {
xSpeedRatio = 0;
}
} else if (cPoint.x > xMaxLimit) {
if (xSpeedRatio > 0) {
xSpeedRatio = 0;
}
}

//Y limits
if (cPoint.y < yMinLimit) {
if (ySpeedRatio < 0) {
ySpeedRatio = 0;
}
} else if (cPoint.y > yMaxLimit) {
if (ySpeedRatio > 0) {
ySpeedRatio = 0;
}
}

//if we dont set speed ration, we give it deafault value to 1
if (_speedRatio == 0) {
_speedRatio = 1;
}

xSpeedRatio = xSpeedRatio * _speedRatio;
ySpeedRatio = ySpeedRatio * _speedRatio;

//version 1.3
if (controlledObject) {
//position object
controlledObject.position = ccp(controlledObject.position.x + xSpeedRatio, controlledObject.position.y + ySpeedRatio);
}
//call protocol method here to gain speed ratios
//check if _delegate conforms to the protocol and responds to the selector
if ([_delegate conformsToProtocol:@protocol(ZJoystickDelegate)]) {
if([_delegate respondsToSelector:@selector(joystickControlDidUpdate:toXSpeedRatio:toYSpeedRatio:)]) {
[_delegate joystickControlDidUpdate:self toXSpeedRatio:xSpeedRatio toYSpeedRatio:ySpeedRatio];
}
}
}
}
Post edited by ad1269 on

Replies

  • ad1269ad1269 Posts: 48New Users @
    edited January 2013
    Also I don't know if this will help, I got this error message and a crash.

    2013-01-18 18:56:22.049 Tile Map[5468:207] *** Assertion failure in -[CCTMXLayer tileGIDAt:withFlags:], /Users/mohanraj/Desktop/TMX Game/Tile Map/Tile Map/libs/cocos2d/CCTMXLayer.m:302
  • Duncan CDuncan C Posts: 9,112Tutorial Authors, Registered Users @ @ @ @ @ @ @
    Is this Cocos2D code? It pays to tell what frameworks you're using, and provide a little background.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.