Assertion failure in +[CCSprite spriteWithSpriteFrameName:]? - xcode

I'm getting an Assertion failure in +[CCSprite spriteWithSpriteFrameName:] for some reason.
I'm basically trying to "updatePinkBerries" or in other words, creating more sprite 'berries' to be added to the screen. They are the 'enemies' in my game.
This is my code:
- (id) init
{
if((self = [super init]))
{
CCLOG(#"%# init", NSStringFromClass([self class]));
self.touchEnabled = YES;
[self scheduleUpdate];
}
return self;
}
-(void)updatePinkBerries:(ccTime)dt
{
CGSize winSize = [CCDirector sharedDirector].winSize;
double curTime = CACurrentMediaTime();
if (curTime > _nextPinkBerrySpawn)
{
// Figure out the next time to spawn an asteroid
float randSecs = randomValueBetween(0.20, 1.0); _nextPinkBerrySpawn = randSecs + curTime;
// Figure out a random Y value to spawn at
float randY = randomValueBetween(0.0, winSize.height);
// Figure out a random amount of time to move from right to left
float randDuration = randomValueBetween(2.0, 10.0);
// Create a new asteroid sprite
CCSprite *pinkBerry = [CCSprite spriteWithSpriteFrameName:#"ship.png"]; [_batchNode addChild:pinkBerry];
// Set its position to be offscreen to the right
pinkBerry.position = ccp(winSize.width + pinkBerry.contentSize.width/2, randY);
// Move it offscreen to the left, and when it's done, call removeNode
[pinkBerry runAction:
[CCSequence actions:
[CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width- pinkBerry.contentSize.width, 0)],
[CCCallFuncN actionWithTarget:self selector:#selector(removeNode:)], nil]];
}
}
EDIT: FULL ASSERTION LOG:
2013-03-31 19:39:09.225 Berry Muncher-iOS[19550:c07] -[CCFileUtils
fullPathForFilename:resolutionType:] : cocos2d: Warning: File not
found: Sprites.plist 2013-03-31 19:39:09.225 Berry
Muncher-iOS[19550:c07] cocos2d: CCSpriteFrameCache: Trying to use file
'Sprites.png' as texture 2013-03-31 19:39:09.425 Berry
Muncher-iOS[19550:c07] cocos2d: CCSpriteFrameCache: Frame 'ship.png'
not found 2013-03-31 19:39:09.426 Berry Muncher-iOS[19550:c07] ***
Assertion failure in +[CCSprite spriteWithSpriteFrameName:],
/Users/Suraya/Desktop/Kobold2D/Kobold2D-2.1.0/Kobold2D/libs/cocos2d-iphone/cocos2d/CCSprite.m:105
EDIT: screenshot of my plist

I recognize this from a Ray Wanderlich tutorial. Because he has different artwork than you might use, you should create your own CCSpriteBatchNode and accompanying plist file so item names from his project don't get mixed up with yours.

Related

SceneKit get current SCNNode position form Animated SCNNode

I want to get a current node position form an animated SCNNode
I am aware of presentationNode and I tried to extract position form there, but no Luck
Here is my Code
SCNNode * pelvis = [_unit childNodeWithName:#"Bip01_Pelvis" recursively:YES];
SCNNode * present = pelvis.presentationNode;
SCNVector3 position = [self convertPosition:present.position toNode:self.parentNode];
The position I have is the position of the SCNNode in the "rest" mode before the CAAnimation was applied.
How to get properties like position form the 'presentationNode'?
Edit
Class itself relevant parts:
#interface UndeadSimple : RepresentationNode <CAAnimationDelegate>
{
//Animations
SCNNode * _undead;
CAAnimation * _currentAnimation;
NSString * _currAnimationkey;
CAAnimation * _death1;
...
SCNNode * _audioNode;
SCNAudioSource * _deathSource;
SCNAudioPlayer * _player;
}
Init:
NSString * sceneName = #ZOMBIE;
SCNScene *scene2 = [SCNScene sceneNamed:sceneName];
_undead = [SCNNode node];
for(SCNNode * node in scene2.rootNode.childNodes)
{
[_undead addChildNode:node];
[node removeAllAnimations];
}
[_undead setScale:(SCNVector3Make(0.024, 0.02, 0.024))];
[self addChildNode:_undead];
[_undead removeAllAnimations];
_currentAnimation = _idleAnimation;
_currAnimationkey = #"resting";
[_undead addAnimation:_currentAnimation forKey:_currAnimationkey];
Animation and event:
SCNAnimationEvent * event2 = [SCNAnimationEvent animationEventWithKeyTime:0.9 block:^(CAAnimation * _Nonnull animation, id _Nonnull animatedObject, BOOL playingBackward)
{
[self.delegate finishedDeathAnimation];
}];
_death1 = anims.death1.mutableCopy;
[_death1 setDelegate:self];
[_death1 setFillMode:kCAFillModeForwards];
_death1.removedOnCompletion = NO;
[_death1 setAnimationEvents:#[event2]];
Animation Call:
- (void)die
{
if([_currAnimationkey isEqualToString:#"dead"])
{
return;
}
[_undead removeAllAnimations];
CAAnimation * death = nil;
int anim = arc4random_uniform(3);
if(anim < 1)
{
death = _death1;
}
else if(anim < 2)
{
death = _death2;
}
else
{
death = _death3;
}
_currAnimationkey = #"dead";
_currentAnimation = death;
}
//Helper Method to get presentation Node
- (SCNNode *)getSnapNode
{
SCNNode * repNode = [_undead presentationNode];
_undead.transform = repNode.transform;
repNode = _undead.clone;
return repNode;
}
//On callback call and try to get position:
- (void)finishedDeathAnimation
{
SCNPlane * bloodgeometry = [SCNPlane planeWithWidth:4 height:4];
bloodgeometry.firstMaterial.diffuse.contents = [NSImage imageNamed:#"blood"];
SCNNode * bloodNode = [SCNNode node];
[bloodNode setEulerAngles:SCNVector3Make(-M_PI_2, 0, 0)];
bloodNode.geometry = bloodgeometry;
//Bip01_Pelvis
SCNNode * deadBody = [_unit getSnapNode];
SCNNode * pelvis = [deadBody childNodeWithName:#"Bip01_Pelvis" recursively:YES];
SCNNode * present = pelvis.presentationNode;
SCNVector3 position = [self convertPosition:present.position toNode:self.parentNode];
NSLog(#"pelvis position %lf,%lf,%lf", present.position.x, present.position.y, present.position.z); //Always {0,0,0}
bloodNode.position = SCNVector3Make(position.x, 0.0001, position.z);
[self.parentNode addChildNode:bloodNode];
[self removeFromParentNode];
}
Edit 2
I also tried to simplify it a bit:
another helper method:
//in undead/unit class
- (SCNVector3)getPosition
{
SCNNode * repNode = [[_undead childNodeWithName:#"Bip01_Pelvis" recursively:YES] presentationNode];
return repNode.position;
}
On callback:
- (void)finishedDeathAnimation
{
position = [_unit getPosition];
NSLog(#"pelvis position %lf,%lf,%lf", position.x, position.y, position.z); //Alway 0,0,0 as well :(
}
It is rare to find someone just like me still believe in Objective C rather than swift thats why i would like to offer full help.
I was having this issue for an animation exported from Blender as a Collada but have succeeded to get the actual position after few tries.
Using presentationNode is the correct way but you need to get it from the child while you making the animation for example this is how to run animation for a node Called _monkey notice that at the last Code i got the _present From the child.presentationNode Not from _monkey.presentationNode Also it have to be obtained during the enumeration not after
so first i need to define
NSNode * _present;
then i do play the animation
[_monkey enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop)
{
for(NSString *key in child.animationKeys)
{ // for every animation key
CAAnimation *animation = [child animationForKey:key]; // get the animation
animation.usesSceneTimeBase = NO; // make it system time based
animation.repeatCount = FLT_MAX; // make it repeat forever
animation.speed = 0.2;
[child addAnimation:animation forKey:key]; // animations are copied upon addition, so we have to replace the previous animation
}
_present = child.presentationNode;
}];
Then Under the render i can check the Results which is changing as the animation moves (Also if you have worked with iOS11 you can check worldPosition instead of position it gives great results too).
- (void)renderer:(id <SCNSceneRenderer>)renderer didSimulatePhysicsAtTime:(NSTimeInterval)time
{
NSLog(#" Location X:%f Y:%f Z:%f", _present.position.x, _present.position.y, _present.position.z);
}
Finally Here Is The Results
Y:-2.505034 Z:4.426160
2017-10-11 04:42:19.700435+0200 monkey[547:137368] Location X:-5.266642 Y:-2.480119 Z:4.427162
2017-10-11 04:42:19.720763+0200 monkey[547:137368] Location X:-5.215315 Y:-2.455228 Z:4.428163
2017-10-11 04:42:19.740449+0200 monkey[547:137346] Location X:-5.163589 Y:-2.430143 Z:4.429171
2017-10-11 04:42:19.760397+0200 monkey[547:137368] Location X:-5.112190 Y:-2.405216 Z:4.430173
2017-10-11 04:42:19.780557+0200 monkey[547:137346] Location X:-5.060925 Y:-2.380355 Z:4.431173
2017-10-11 04:42:19.800418+0200 monkey[547:137342] Location X:-5.008560 Y:-2.355031 Z:4.432024
This Give Me Good Results. Hope it would work with you.
There's no animation running, at least not in the code you've shown us. Check the presentation node's position while the animation is playing, perhaps in renderer:updateAtTime: (see SCNRendererDelegate).

Detecting and ignoring touch on non-transparent part of MKOverlay drawn Image in iOS8

I have a overlay image (.png) on my map that consists of a transparent bit in the middle, and colored sides so the user can only focus on the middle part. However do to the shape of that middle bit, quite a bit is visible at some sides.
I'm trying to detect a tap on the OverlayView so I can ignore it and only accept touches in the designated area.
I followed the following tut at Ray Wenderlich's site for adding the overlay:
The image overlay is drawn like this:
#implementation PVParkOverlayView
- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage {
self = [super initWithOverlay:overlay];
if (self) {
_overlayImage = overlayImage;
}
return self;
}
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGImageRef imageReference = self.overlayImage.CGImage;
//UIImage *imageTest = _overlayImage;
MKMapRect theMapRect = self.overlay.boundingMapRect;
CGRect theRect = [self rectForMapRect:theMapRect];
//orientation testing
//CGContextRotateCTM(context, 0);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);
}
I have a gesture recognizer on my mapview and am trying to detect the tap there:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint tapPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D tapCoord = [self.mapView convertPoint:tapPoint toCoordinateFromView:self.mapView];
MKMapPoint mapPoint = MKMapPointForCoordinate(tapCoord);
CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
for (id<MKOverlay> overlay in self.mapView.overlays) {
if([overlay isKindOfClass:[PVParkOverlay class]]){
NSLog(#"overlay is present");
/*
MKPolygon *polygon = (MKPolygon*) overlay;
CGMutablePathRef mpr = CGPathCreateMutable();
MKMapPoint *polygonPoints = polygon.points;
for (int p=0; p < polygon.pointCount; p++){
MKMapPoint mp = polygonPoints[p];
if (p == 0)
CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
else
CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
}
if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, FALSE)){
// ... found it!
NSLog(#"I've found it!");
}
//CGPathRelease(mpr);
*/
}
}
I know that the overlay is there, but since it is a drawn image I can't find a way to convert this to polygon points to use this code (if even possible).
Any other methods I can use for this?
I also found following sample code but the viewForOverlay method is deprecated:
- (void)mapTapped:(UITapGestureRecognizer *)recognizer
{
MKMapView *mapView = (MKMapView *)recognizer.view;
id<MKOverlay> tappedOverlay = nil;
for (id<MKOverlay> overlay in mapView.overlays)
{
MKOverlayView *view = [mapView viewForOverlay:overlay];
if (view)
{
// Get view frame rect in the mapView's coordinate system
CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
// Get touch point in the mapView's coordinate system
CGPoint point = [recognizer locationInView:mapView];
// Check if the touch is within the view bounds
if (CGRectContainsPoint(viewFrameInMapView, point))
{
tappedOverlay = overlay;
break;
}
}
}
NSLog(#"Tapped view: %#", [mapView viewForOverlay:tappedOverlay]);
}

CCRenderTexture and threads warning on OS X

I am getting this warning with Cocos2D 2.0 on OS X:
-[CCRenderTexture initWithWidth:height:pixelFormat:depthStencilFormat:] : cocos2d: WARNING. CCRenderTexture is running on its own thread. Make sure that an OpenGL context is being used on this thread!
Here's the code that I believe is causing the warning:
- (id) initWithObject: (CCNode *) object mask: (CCSprite *) mask {
NSAssert(object != nil, #"Invalid sprite for object");
NSAssert(mask != nil, #"Invalid sprite for mask");
if((self = [super init])) {
_objectSprite = object;
_maskSprite = mask;
// Set up the burn sprite that will "knock out" parts of the darkness layer depending on the alpha value of the pixels in the image.
[_maskSprite setBlendFunc: (ccBlendFunc) { GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }];
// Get window size, we want masking over entire screen don't we?
CGSize size = [[CCDirector sharedDirector] winSize];
// Create point with middle of screen
CGPoint screenMid = ccp(size.width * 0.5f, size.height * 0.5f);
// Create the rendureTextures for the mask
_masked = [CCRenderTexture renderTextureWithWidth: size.width height: size.height];
[[_masked sprite] setBlendFunc: (ccBlendFunc) { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }];
// Set render textures at middle of screen
_masked.position = screenMid;
// Add the masked object to the screen
[self addChild: _masked];
[[CCDirector sharedDirector] setAlphaBlending:YES];
}
return self;
}
Please help, I just cannot figure this out.
In ccConfig.h, find a line that says
#define CC_DIRECTOR_MAC_THREAD CC_MAC_USE_DISPLAY_LINK_THREAD
This is where the use of threads is defined. You can try using the main thread option (CC_MAC_USE_MAIN_THREAD), or use GCD to make sure all code is actually executed on -[CCDirector runningThread]. This will pretty much fix it.

CoreGraphics drawRect in a cocos2d project EXEC BAD ACCESS

I got a interesting riddle here. My cocos2d project uses a UIView to display nicer popups than the regular alert view. To be more flexible in the size of the popup, I draw the background in the drawRect method.
But first the hierarchy how I implement cocos2d and UIKit:
Every UIKit element is added to the RootViewController. Every CCNode to the EAGLView. (If someone has a better solution to mix those world, don't wait to tell me!) Unfortunately is every UIKit view in front of cocos nodes.
Everything works fine when I add the first popup to the RootViewController. But if I remove the first popup and add a new one to the RootViewController occurs a bad access.
It crashes only in combination with cocos2d.
I use the code from Ray Wenderlichs CoreGraphics 101 tutorial.
context and strokeColor are not nil.
Another important infos: I use ARC and am supporting iOS 4.2 and above.
The complete code can be found at raywenderlich.com or below
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
CGRect rectFor1PxStroke(CGRect rect)
{
return CGRectMake(rect.origin.x + 0.5, rect.origin.y + 0.5, rect.size.width - 1, rect.size.height -1);
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect frame = CGRectInset(self.bounds, 2, 2);
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 3.0, shadowColor);
CGRect strokeRect = CGRectInset(frame, -2.0, -2.0);
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetLineWidth(context, 2.0);
CGContextStrokeRect(context, rectFor1PxStroke(strokeRect));
drawLinearGradient(context, frame, gradient1, gradient2);
}
ARC releases the CGColorRef named strokeColor. One fix is to replace it with an CGFloat array and use CGContextSetStrokeColor instead CGContextSetStrokeColorWithColor
This answer solves this issue:
App crashes when using __bridge for CoreGraphics gradient on ARC
I agree with zeiteisen ,ARC releases CGColorRef and the easiest way to solve this by
replacing CGContextSetStrokeColorWithColor with
CGContextSetStrokeColor(context, components)
Change UIColor of strokeColor to 'RGB CGFloat components array' as following:
static CGFloat red, green, blue, alpha;
- (void)getRGBComponents:(CGFloat [4])components forColor:(UIColor *)color {
[color getRed:&red green:&green blue:&blue alpha:&alpha];//To fetch CGFloat RGB components
for (int component = 0; component < 4; component++) {
switch (component) {
case 0:
components[component] = red;
break;
case 1:
components[component] = green;
break;
case 2:
components[component] = blue;
break;
case 3:
components[component] = alpha;
break;
default:
break;
}
}
}
Use this method like this:
CGFloat components[4];
UIColor *strokeColor=[UIColor greyColor];//My color
[self getRGBComponents:components forColor:strokeColor];//'components' will be substituted in CGContextSetStrokeColor
NSLog(#"%f %f %f %f", components[0], components[1], components[2],components[3]);

Cocos2d: Animation based on accelerometer

Anyone know of any good up to date tutorials out there that show how can one animate a sprite based on accelerometer movement. I want to animate a bird to sway to the position the device was pointed to. For example if the player decides to move the bird to the left via the accelerometer I would like for my bird to play an animation that is swaying to the left.
// Accelerometer
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
birdSpeedY = 9.0 + acceleration.x*15;
birdSpeedX = -acceleration.y*20;
}
// Updating bird based on accelerometer
-(void)updateBird {
float maxY = winSize.height - bird.contentSize.height/2;
float minY = bird.contentSize.height/2;
float newY = bird.position.y + birdSpeedY;
newY = MIN(MAX(newY, minY), maxY);
float maxX = winSize.width - bird.contentSize.width/2;
float minX = bird.contentSize.width/2;
float newX = bird.position.x + birdSpeedX;
newX = MIN(MAX(newX, minX), maxX);
bird.position = ccp(newX, newY);
}
// Making background scroll automatically
-(void)update:(ccTime)dt {
[self updateBird];
CGPoint backgroundScrollVel = ccp(-100, 0);
parallaxNode.position = ccpAdd(parallaxNode.position, ccpMult(backgroundScrollVel, dt));
}
-(id)init {
self = [super init];
if (self != nil) {
winSize = [CCDirector sharedDirector].winSize;
CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:#"birdAtlas.plist"];
NSMutableArray *framesArray=[NSMutableArray array];
for (int i=1; i<10; i++) {
NSString *frameName=[NSString stringWithFormat:#"bird%d.png", i];
id frameObject=[cache spriteFrameByName:frameName];
[framesArray addObject:frameObject];
}
// animation object
id animObject=[CCAnimation animationWithFrames:framesArray delay:0.1];
// animation action
id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
animAction=[CCRepeatForever actionWithAction:animAction];
bird=[CCSprite spriteWithSpriteFrameName:#"bird1.png"];
bird.position=ccp(60,160);
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:#"birdAtlas.png"];
[self addChild:batchNode z:100];
[batchNode addChild:bird];
[bird runAction:animAction];
self.isAccelerometerEnabled = YES;
[self scheduleUpdate];
[self addScrollingBackgroundWithTileMapInsideParallax];
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
#end
You can try the Accelerometer methods with it and change the position of the Sprite using ccp(). You also need to know is that project for the Landscape or Portrait in the Mode.
You Can Try the Stuff below
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
[lbl setString:[NSString stringWithFormat:#"X=>%.2lf Y=>%.2lf",(double)acceleration.x,(double)acceleration.y]];
double x1= -acceleration.y *10;
double y1= acceleration.x *15;
if(acceleration.x >0.05)
{
y1*=spped_incr; // Make Movement Here
}
[Sprite_Name runAction:[CCMoveTo actionWithDuration:0.1f position:ccpAdd(ccp(x1,y1), Sprite_Name.position)]];
}
The Above Stuff is for the Landscape Mode.... if You need in Portrait Mode you need to change the Axis and use the TRY & Error Method.

Resources