I have a subclass of cpCCSprite, which is just for using chipmunk spaceManager and my class looks like this:
#implementation Helmet
+(id) helmetWithGame:(Game*)game {
return [[[self alloc] helmetInit:game] autorelease];
}
- (id) helmetInit:(Game*)game {
cpShape *helmet_1;
cpShape *helmet_2;
cpShape *helmet_3;
cpShape *reference;
reference = [game.spaceManager addCircleAt:cpvzero mass:STATIC_MASS radius:2];
helmet_1 = [game.spaceManager addCircleToBody:reference->body radius:20 offset:cpv(-5, 2)];
helmet_2 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(16, -14)];
helmet_3 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(8, -14)];
reference->group =1;
helmet_1->group =1;
helmet_2->group =1;
helmet_3->group =1;
[self initWithFile:#"Helmet.png"];
[self setBody:reference->body];
self.spaceManager = game.spaceManager;
self.autoFreeShapeAndBody = YES;
gameScreenSize = game.contentSize;
return self;
}
- (void) generateAndShowOn:(Game *)game {
float startX = (float)((arc4random_uniform(101)) + 100);//returns number from 100 to 200 and casts it as a float
float startY;
int endXRange = (game.contentSize.width * .8) - (game.contentSize.width * .5);
float endX = (float)((arc4random_uniform(endXRange)) + (game.contentSize.width * .5));
float endY;
BOOL shouldStartTop;
if ((arc4random_uniform(101)) < 50) {//returns number from 0 to 100 and checks to see if it is less than 50. If it is, than the helmut starts at the top
shouldStartTop = YES;
startY = game.contentSize.height + (self.contentSize.height * .5);
endY = -self.contentSize.height;
}
else {
shouldStartTop = NO;
startY = -(self.contentSize.height * .5);
endY = game.contentSize.height + self.contentSize.height;
}
self.position = ccp(startX, startY);
[game addChild:self];
ccBezierConfig bezier;
bezier.controlPoint_1 = ccp (startX, startY);
bezier.controlPoint_2 = ccp (endX, endY);
bezier.endPosition = ccp (endX, endY);
id rotate =[CCRotateBy actionWithDuration:1.5f angle:360];
id curve = [CCBezierTo actionWithDuration:1.5f bezier:bezier];
id spawn = [CCSpawn actions:rotate, curve, nil];
[self runAction:spawn];
[self schedule:#selector(doneAnimating) interval:1.6];
}
- (void) doneAnimating{
if ([[self delegate]respondsToSelector:#selector(helmetPastBounds:)]) {
[[self delegate]helmetPastBounds:self];
}
}
- (void) dealloc {
CCLOG(#"%s", __PRETTY_FUNCTION__);
[super dealloc];
}
#end
So a new instance of this class is called every second, so that there can be more than one helmet on the screen at a time. Then I have another method that is called just slightly after the acctions have finished to remove the current helmet and deallocate it. But the problem I am having is every so often the actions are jumpy/jerky and that is with the FPS at 60. Do I need to do something different with the actions or what could be causing this?
Related
How do we enumerate and alter our object's position (contained in array) for each delta time?
I put some CCsprite objects inside array, then I displayed them in scene, but also I wanted to make them move with modifying update method, I failed on last part.
How do I get around this ?
#implementation GameScene
{
Hunter *_hunter;
Bird *_bird;
NSMutableArray *_arrayOfBirds;
}
-(void)update:(CCTime)delta{
CGSize viewSize = [CCDirector sharedDirector].viewSize;
float birdSpeed = 50;
for (Bird *birds in _arrayOfBirds) {
if (birds.position.x < 0) {
birds.flipX = YES;
}
if (birds.position.x > viewSize.width) {
birds.flipX = NO;
}
float distanceToMove = birdSpeed * delta;
float direction = birds.flipX ? 1 : -1;
float newX = birds.position.x + direction * distanceToMove;
float newY = birds.position.y;
birds.position = ccp(newX, newY);
}
}
-(void)addBird{
CGSize viewSize = [CCDirector sharedDirector].viewSize;
for (int i=0; i < 4; i++) {
_bird = [[Bird alloc]initWithBirdType:(i)];
_bird.position = ccp(viewSize.width * 0.5f + 30 * i , viewSize.height * 0.9f - 15* i);
[self addChild:_bird];
[_arrayOfBirds addObject:_bird];
}
}
You forgot to initialize your array
here (assuming ARC)
-(id) init {
if(self=[super init]) {
_arrayOfBirds = [[NSMutableArray alloc] init];
// the rest
}
return self;
}
I used CCEffectGlass in Cocos2d 3.2, it works good but I see double layer Or no layer at end like in image. Is it possible to hide double layer only inside circle ?
CODE:
+(void)showGlassEffectForSprite:(CCSprite*)effectBG parent:(CCNode*)inParent
{
CCSpriteFrame *normalMap = [CCSpriteFrame frameWithImageNamed:#"gtEffectsImage.png"];
CCSprite *reflectEnvironment = [CCSprite spriteWithImageNamed:#"gtEffectsGlass.jpg"];
reflectEnvironment.positionType = CCPositionTypeNormalized;
reflectEnvironment.position = ccp(0.5f, 0.5f);
reflectEnvironment.visible = NO;
[inParent addChild:reflectEnvironment];
CCEffectGlass *glass = [[CCEffectGlass alloc] initWithShininess:1.0f refraction:1.0f refractionEnvironment:effectBG reflectionEnvironment:reflectEnvironment];
glass.fresnelBias = 0.1f;
glass.fresnelPower = 2.0f;
glass.refraction = 0.75f;
CCSprite *sprite1 = [[CCSprite alloc] init];
sprite1.position = ccp(SW*0.1f, effectBG.position.y);
sprite1.normalMapSpriteFrame = normalMap;
sprite1.effect = glass;
sprite1.scale = 0.8f;
sprite1.colorRGBA = [CCColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
float duration = 4.0f ;
[sprite1 runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
[CCActionMoveTo actionWithDuration:duration position:ccp(SW*1.4f, effectBG.position.y)],
[CCActionDelay actionWithDuration:1.0f],
[CCActionMoveTo actionWithDuration:duration position:ccp(-SW*0.4f, effectBG.position.y)],
[CCActionDelay actionWithDuration:1.0f],
nil
]]];
[inParent addChild:sprite1 z:5];
}
FULL Xcode Source : https://github.com/Gururajtallur/CCEffectGlassSample
Im programming a game using sprite kit and i have written the accelerometer code and it works. But the problem is it doesn't stop when hitting the edge of the screen, can anyone help me out with this?
Here is the code for the player:
-(void)addShip
{
//initalizing spaceship node
ship = [SKSpriteNode spriteNodeWithImageNamed:#"Spaceship"];
[ship setScale:0.5];
ship.zRotation = - M_PI / 2;
//Adding SpriteKit physicsBody for collision detection
ship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ship.frame.size];
ship.physicsBody.mass = 0.02;
ship.physicsBody.categoryBitMask = shipCategory;
ship.physicsBody.dynamic = YES;
ship.physicsBody.affectedByGravity = NO;
ship.physicsBody.contactTestBitMask = DonutCategory | PizzaCategory | ChocolateCategory | SoftCategory | AppleCategory | GrapeCategory | OrangeCategory | BananaCategory;
ship.physicsBody.collisionBitMask = 0;
ship.physicsBody.usesPreciseCollisionDetection = YES;
ship.name = #"ship";
ship.position = CGPointMake(260,30);
[self addChild:ship];
motionManager = [[CMMotionManager alloc] init];
if ([motionManager isAccelerometerAvailable] == YES) {
[motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *data, NSError *error)
{
float destX, destY;
float currentX = ship.position.x;
float currentY = ship.position.y;
BOOL shouldMove = NO;
if(data.acceleration.x < -0.25) { // tilting the device to the right
destX = currentX + (data.acceleration.x * kPlayerSpeed);
destY = currentY;
shouldMove = YES;
} else if (data.acceleration.x > 0.25) { // tilting the device to the left
destX = currentX + (data.acceleration.x * kPlayerSpeed);
destY = currentY;
shouldMove = YES;
}
if(shouldMove) {
SKAction *action = [SKAction moveTo:CGPointMake(destX, destY) duration:1];
[ship runAction:action];
}
}];
}
}
You can add edges to your screen so the physics will stop the ship when it hits the edge (do it in init method):
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
//Add this to change some behaviour when ship will collide with the screen
//self.physicsBody.friction = 0.0f;
The other way is check in update method when ship is touching edge of the screen and change the ship position.
//Extended
I believe moveTo:duration: method mess up your phasic, to fix it just mate sure your destX and destX to go more that screen size width (height) - ship size width (height) and less that origin x and origin y.
You shouldn't use moveTo:duration: method, instead you should apply force to your ship.
Try this code, it's a little bit different that yours but this is much better way to move your ship (ignore the code above):
//Your ship setting
_ship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_ship.frame.size];
_ship.physicsBody.mass = 0.02;
_ship.physicsBody.categoryBitMask = shipCategory;
_ship.physicsBody.dynamic = YES;
_ship.physicsBody.affectedByGravity = NO;
// Edge around the screen
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
//Apply movement (instead of moveTo:duration:)
//Get the accelerometer value
CMAccelerometerData* accelData = _motionManager.accelerometerData;
if (fabs(accelData.acceleration.x) > 0.2) {
// 35 is the value you can play with to make your ship movement feel more natural
[_ship.physicsBody applyForce:CGVectorMake(0.0, 35.0 * data.acceleration.x)];
}
I am using the ccTouchBegan and ccTouchEnded, and for some reason there is a block of code that runs perfectly on iPhone simulator and doesn't run at all on iPhone 4. This is what my methods look like:
-(BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event {
firstTouch = [self convertTouchToNodeSpace:touch];
if (!self.currentFootball.footballHasEmbarked) {
//arrow.position = ccp(fPos.x, fPos.y);
float newAnc = (120 + (self.currentFootball.contentSize.width * .5f) + (arrow.contentSize.width * .5f)) / arrow.contentSize.width;
arrow.anchorPoint = ccp(-newAnc, .5);//self.currentFootball.position;
arrow.position = self.currentFootball.position;//ccp(fPos.x-120, fPos.y);
arrow.rotation = 180;
arrow.visible = YES;
arrow.scale = 0.5f;
//powerLevel.position = fPos;
powerLevel.position = self.currentFootball.position;
powerLevel.rotation = 180;
powerLevel.percentage = 0;
powerLevel.visible = YES;
outLine.position = self.currentFootball.position;
outLine.rotation = 180;
outLine.visible = YES;
CCProgressFromTo *powerBarGoUp = [CCProgressFromTo actionWithDuration:1.0f from:0.0f to:100.0f];
CCProgressFromTo *powerBarGoDown = [CCProgressFromTo actionWithDuration:1.0f from:100.0f to:0.0f];
id action = [CCRepeatForever actionWithAction:[CCSequence actions:powerBarGoUp, powerBarGoDown, nil]];
[powerLevel runAction:action];
return YES;
}
else {
return NO;
}
}
-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event {
const float max = 100;
CGPoint endTouch = [self convertTouchToNodeSpace:touch];
if (endTouch.x > firstTouch.x) {
endTouch = ccp(firstTouch.x, endTouch.y);
//CCLOG(#"I ran");
}
arrow.visible = NO;
powerLevel.visible = NO;
outLine.visible = NO;
self.currentFootball.footballHasEmbarked = YES;
self.currentFootball.spiraling = YES;
if (self.currentFootball) {
[smgr addBody:self.currentFootball.body];
}
if (CGPointEqualToPoint(endTouch, firstTouch)) {
CCLOG(#" I have run");
float anchorPointDist = ccpLength(endTouch);
float distFactor = distFromFb + anchorPointDist;
projectionAnchorPoint = ccp(firstTouch.x + distFactor,firstTouch.y);
}
CGPoint diff = ccpSub(endTouch, projectionAnchorPoint);
float len = powerLevel.percentage;
CGPoint norm = ccpNormalize(diff);
if (len > max){
len = max;
}
[self.currentFootball applyImpulse:ccpMult(norm, (len * 300))];
pos = self.currentFootball.position.y;
[self schedule:#selector(newFootball)];
}
This is the block of code that will not run on my iPhone.
if (CGPointEqualToPoint(endTouch, firstTouch)) {
CCLOG(#" I have run");
float anchorPointDist = ccpLength(endTouch);
float distFactor = distFromFb + anchorPointDist;
projectionAnchorPoint = ccp(firstTouch.x + distFactor,firstTouch.y);
}
Am I not doing something right?
Print out the values for endTouch and firstTouch. They may vary by a very small amount on device, as it's harder to keep your finger in the same location as opposed to a mouse pointer. If that's the case, you may want to accept end touches within a range of the first touch.
Perhaps there is some slight difference in the points you are comparing, making the result of that comparison always false. Try using this method instead of CGPointEqualToPoint.
BOOL ccpFuzzyEqual(CGPoint a, CGPoint b, float variance);
I drew a line with -(void)draw method:
-(void)draw // code for line draw
{
glEnable(GL_LINE_SMOOTH);
CGPoint start;
start.x = 50;
start.y = 50;
CGPoint end;
end.x = 50;
end.y = 200;
if (pointOne.x>300){
pointOne.x = 300;
}
if (pointOne.y>200){
pointOne.y = 200;
}
ccDrawLine(start, pointOne);//get a line
[self Bezier:location.x:location.y:pointOne.x:pointOne.y];
}
and now I want to curve this line through Bezier in cocos2d. When I move finger that time line should draw the curve.
Bezier Code is:
- (void) Bezier:(NSInteger) CP_x:(NSInteger) CP_y:(NSInteger) end_x:(NSInteger) end_y
{
CGPoint start;
start.x = 50;
start.y = 50;
ccBezierConfig bezier;
bezier.controlPoint_1 = ccp(CP_x, CP_y);
bezier.controlPoint_2 = ccp(CP_x,CP_y);
bezier.endPosition = ccp(end_x,end_y);
}
How can I implement this line in bezier?
try this.
CCDrawBezier in last of your -(void)Bezier methord
ccDrawCubicBezier(StartPoint, controlPoint_1, controlPoint_2, EndPoint,NSInteger);
This is a method is wrote to my game.
//Bezier calculation on multiple points
//Can be set to any number of points
//The t parameter is the time 0 = start point -> 1 = end point.
//Made by Sebastian Winbladh
-(CGPoint)getBezerAtTime:(float)t array:(NSArray*)a{
int count = [a count];
float xPoints[count],yPoints[count];
int c=0;
//Makeup an array for our points
for(int i=0;i<count;i++){
CGPoint p = CGPointMake([[[a objectAtIndex:i] objectAtIndex:0] floatValue], [[[a objectAtIndex:i] objectAtIndex:1] floatValue]);
xPoints[i] = p.x;
yPoints[i] = p.y;
c++;
}
//Calculate our bezier curve here
while(c != 0){
for(int i=0;i<c-1;i++){
CGPoint p0 = CGPointMake(xPoints[i], yPoints[i]);
CGPoint p1 = CGPointMake(xPoints[i+1], yPoints[i+1]);
float dx = p1.x - p0.x;
float dy = p1.y - p0.y;
dx = p0.x + (dx * t);
dy = p0.y + (dy * t);
xPoints[i] = dx;
yPoints[i] = dy;
}c--;
}
return CGPointMake(xPoints[0], yPoints[0]);
}