CCSprite not appearing in CCLayerColor - ios

i have a ccsprite which i would like to place inside a CCLayerColor but for some reason its not showing up inside of it. i have the following code and i'm stumped why it isn't showing up.
[self setColor:ccGREEN];
[self setOpacity:255];
[self setPosition:(CGPointMake(0, 60/2+bottomPadding))];
self.inventoryHolder = [[CCSprite alloc] init];
[self.inventoryHolder setTextureRect:CGRectMake(0, 0, 100, 60)];
[self.inventoryHolder setColor:ccc3(255, 0, 0)];
self.inventoryHolder.anchorPoint = ccp(0, 0);
self.inventoryHolder.position = ccp(0, 60/2+50);
[self addChild:self.inventoryHolder z:100];
self being the CCLayerColor and self.inventoryHolder being the CCSprite. Any help would be awesome!

You haven't actually set a texture in the sprite, so there's nothing for it to display.

Related

iOS: how to move ball

Hi how do i create a moving ball on iOS.
When the program starts, I will display the ball on the left of the screen. Subsequently, each time i click a UIButton, how do i move the ball to the right on the same x-axis.
I managed to display the ball but how do i update and redraw its location when the UIButton is pressed? currently each time i press the UIButton, it creates a new ball and the old balls are not cleared.
i understand its because i recreate a new instance of the ball. So how shall i go about fixing this?
Here is my code....
ballView.m
#implementation BallView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
-(void)drawRect:(CGRect)dirtyRect
{
NSLog(#"in drawRect");
// Get the graphics context and clear it
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Draw a solid ball
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextFillEllipseInRect(ctx, dirtyRect);
[self setNeedsDisplay];
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
MoveBallController.m
-(void)moveBall
{
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
BallView *ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:ball];
}
Create one BallView instance and store it in an instance variable. Then when the button is clicked you simply update the ball's frame:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
_ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:_ball];
}
- (void)moveBall {
CGRect frame = _ball.frame;
frame.origin.x += 5; // use some appropriate increment
_ball.frame = frame;
}
where _ball is your new instance variable.

UISnapBehavior Causing Distortion

For whatever reason, whenever I use the snapping behavior, it causes the UIButtons to turn into rectangles of various sizes when "snapped". I'm probably just looking right over the issue. My deadline is tomorrow to get the last bugs worked out, so I would GREATLY appreciate ANY help! Thank you!
vvv - ViewController.m - vvv
//set points
CGPoint bullyboxPoint = {80, 188};
CGPoint weemPoint = {240, 188};
CGPoint yeaPoint = {80, 339};
CGPoint charityPoint = {240, 339};
CGPoint phhsPoint = {80, 488};
CGPoint experiencePoint = {240, 488};
//make the buttons closer to speed up the snap
bullybox.frame = CGRectMake(40, 788, 120, 120);
weem.frame = CGRectMake(200, 788, 120, 120);
yea.frame = CGRectMake(30, 939, 120, 120);
charity.frame = CGRectMake(190, 939, 120, 120);
phhs.frame = CGRectMake(20, 1328, 120, 120);
experience.frame = CGRectMake(180, 1328, 120, 120);
//create behaviors
UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:self->bullybox snapToPoint:bullyboxPoint];
UISnapBehavior *snapBehavior1 = [[UISnapBehavior alloc] initWithItem:self->weem snapToPoint:weemPoint];
UISnapBehavior *snapBehavior2 = [[UISnapBehavior alloc] initWithItem:self->yea snapToPoint:yeaPoint];
UISnapBehavior *snapBehavior3 = [[UISnapBehavior alloc] initWithItem:self->charity snapToPoint:charityPoint];
UISnapBehavior *snapBehavior4 = [[UISnapBehavior alloc] initWithItem:self->phhs snapToPoint:phhsPoint];
UISnapBehavior *snapBehavior5 = [[UISnapBehavior alloc] initWithItem:self->experience snapToPoint:experiencePoint];
//dampen the snap
snapBehavior.damping = 1.2f;
snapBehavior1.damping = 1.2f;
snapBehavior2.damping = 1.2f;
snapBehavior3.damping = 1.2f;
snapBehavior4.damping = 1.2f;
snapBehavior5.damping = 1.2f;
//add behaviors
[self.animator addBehavior:snapBehavior];
[self.animator addBehavior:snapBehavior1];
[self.animator addBehavior:snapBehavior2];
[self.animator addBehavior:snapBehavior3];
[self.animator addBehavior:snapBehavior4];
[self.animator addBehavior:snapBehavior5];
The problem is probably that you're using Autolayout. Autolayout and UIKit Dynamics are enemies (if you look at Apple's example code, you'll see that they have circumvented this issue by turning Autolayout off).
The way I like to solve this problem is: Make snapshot images of the objects to be animated; hide the actual objects and put the snapshot images in their place; animate the snapshot images; when it's all over, take the snapshot images out of the interface and show the actual objects in their new location.
This works because the snapshot images are not subject to Autolayout. However, you will still need to grapple with the issue, because you cannot move buttons that are subject to Autolayout by setting their frame; you must set their constraints.

Getting a weird error, I'm using cocos2d

Im getting This Error Instance method '-setTouchEnabled:' not found (return type defaults to 'id')
I don't know whats causing it, any help would be greatly appreciated :)
Heres my code sorry if its messy
- (id) init
{
if ((self = [super init])) {
CGSize winSize = [CCDirector sharedDirector].winSize;
CCSprite *player = [CCSprite spriteWithFile:#"start.png"];
player.position = ccp(player.contentSize.width/2, winSize.height/2);
[self addChild:player];
[self schedule:#selector(gameLogic:) interval:0.5];
[self setTouchEnabled:YES];
_monsters = [[NSMutableArray alloc] init];
_projectiles = [[NSMutableArray alloc] init];
// stuff that needs updating
[self schedule:#selector(update:)];
//labels
StrategyBullet = 10;
Strategyscore = 0;
CCLabelTTF *label2 = [CCLabelTTF labelWithString:#"Ninja stars " fontName:#"Arial" fontSize:15];
label2.position = ccp(400,310);
label2.color = ccc3(255, 0, 0);
[self addChild: label2 z:1];
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Score" fontName:#"Arial" fontSize:15];
label.position = ccp(30,310);
label.color = ccc3(255, 0, 0);
[self addChild: label z:1];
StrategyscoreLabel = [CCLabelTTF labelWithString:#"0" fontName:#"Arial" fontSize:14];
StrategyscoreLabel.position = ccp(65, 310);
StrategyscoreLabel.color = ccc3(255, 255, 255);
[self addChild:StrategyscoreLabel z:1];
StrategyBulletLabel = [CCLabelTTF labelWithString:#"0" fontName:#"Arial" fontSize:14];
StrategyBulletLabel.position = ccp(450, 310);
StrategyBulletLabel.color = ccc3(255, 255, 255);
[self addChild:StrategyBulletLabel z:1];
}
return self;
}
The setter you're looking for would look like this.-
[self setIsTouchEnabled:YES];
(notice the missing 'Is'). Or you can directly write
self.isTouchEnabled = YES;
EDIT
As #Grzegorz Krukowski said, setIsTouchEnabled is currently deprecated and setTouchEnabled is actually the method used in cocos2d 2.x. My guess is that you may be using a version prior 2.x.

compare two CCSprite

_ball = [CCSprite spriteWithFile:#"ball.png" rect:CGRectMake(0, 0, 52, 52)];
_ball.position = ccp(300, 300);
[self addChild:_ball];
_ball1 = [CCSprite spriteWithFile:#"ball.png" rect:CGRectMake(0, 0, 52, 52)];
_ball1.position = ccp(300, 300);
[self addChild:_ball1];
if (_ball1.position.x == _ball.position.x && _ball1.position.y == _ball.position.y) {
NSLog(#"Stop");
}
if _ball is touch _ball1 not worrking
in this something wrong?
Use CGRectIntersectsRect to determine if the sprites overlap:
if (CGRectIntersectsRect([_ball1 boundingBox], [_ball2 boundingBox])) {
NSLog(#"Stop");
}
Of course, this is just a rectangular intersection, which is probably unsuitable for circular objects, in which you can use the ideas presented in this webpage.

Touch accuracy of ccmenuitem is lower when it is placed adjacent to screen edge

When I tried to build and run my game(based on Cocos2d 1.0.1, built in Xcode 4.5 with iOS 6.0 SDK) on iTouch 5, I found the CCMenuItems do not behave normally: when the menuitem is adjacent to screen edge, the edge border seems not so easy to be tapped inside to respond the touch event(Sorry for my poor expression).
To demonstrate the problem, I wrote a demo app with Xcode 4.3 using Cocos2d template and just modify the HelloWorldLayer's init method, and the phenomenon still happens. The code is below:
-(void) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
CCLayerColor *cl = [CCLayerColor layerWithColor:ccc4(ccWHITE.r, ccWHITE.g, ccWHITE.b, 255)];
[self addChild:cl];
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Hello World" fontName:#"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
float width = 160;
CCSprite *sp1 = [CCSprite node];
[sp1 setContentSize:CGSizeMake(width, width)];
[sp1 setTextureRect:CGRectMake(0, 0, width, width)];
[sp1 setColor:ccc3(0xff, 0xff, 0)];
CCSprite *sp2 = [CCSprite node];
[sp2 setContentSize:CGSizeMake(width, width)];
[sp2 setTextureRect:CGRectMake(0, 0, width, width)];
[sp2 setColor:ccc3(0, 0, 0xff)];
CCMenuItemSprite *button = [CCMenuItemSprite itemFromNormalSprite:sp1 selectedSprite:sp2 target:nil selector:nil];
CCMenu *menu = [CCMenu menuWithItems:button, nil];
[self addChild:menu];
menu.position = ccp(0, 0);
button.anchorPoint = ccp(1, 1);
button.position = ccp([[CCDirector sharedDirector] winSize].width,
[[CCDirector sharedDirector] winSize].height);
}
return self;
}
I looked all over the internet and had no luck, wonder someone can help me with that. Many Thanks!
Just a few hunches:
Refrain from setting the sprites contentSizes. They should be set automatically, and may be used by CCMenu.
Make sure you don't change position or anchorPoint of CCMenuItem. Don't change anchorPoint of CCMenu either. This will mess up touch detection. Only use position property of CCMenu.
Make sure no other touch code elsewhere in your project interferes and may be swallowing touches. Gesture Recognizers can also cause CCMenu to misbehave.
If you want to freely position your menu items, make sure to wrap each in a CCMenu node. Then you can position the item via the menu.

Resources