ContactListener box2d cocos2d contacted body property - cocos2d-iphone

I have Building CCNode
It's have property:
in .h file
#property (nonatomic) int testProperty;
in .mm file add tag and set property
[[GB2ShapeCache sharedShapeCache] addShapesWithFile:#"Objects.plist"];
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
batchNode = [CCSpriteBatchNode batchNodeWithFile:#"sprite.png"];
[self addChild:batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"sprite.plist"];
sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:#"%#.png", type]];
[batchNode addChild:sprite];
[sprite setTag:2]; //SET TAG
[self setTestProperty:10]; //SET PROPERTY
...
Also is the ContactListener class
It's work correctly with this code and i'm detecting body by tag fine:
#import "ContactListener.h"
#import "Building.h"
void ContactListener::BeginContact(b2Contact* contact)
{
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
Building *buildA = (Building *)bodyA->GetUserData();
Building *buildB = (Building *)bodyB->GetUserData();
if (buildA.tag == 2) {
NSLog(#"Collision with building");
}
}
PROBLEM:
I don understand how get property from ContactListener
I tried to get it so:
if (buildA.tag == 2) {
NSLog(#"Collision with building");
NSLog(#"testProperty == %i", buildA.testProperty);
}
But buildA.testProperty not work, and get error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite testProperty]: unrecognized selector sent to instance 0x434af0'
If your can explain my please how get property from this class. Thanks.

'-[CCSprite testProperty]: unrecognized selector sent to instance 0x434af0'
You store the CCSprite in userdata, not the Building object.
You set the tag like this, then of course checking for the correct tag works:
[sprite setTag:2];
But this is incorrect because user data is not a Building but a sprite:
Building *buildA = (Building *)bodyA->GetUserData();
I assume that Building inherits from CCSprite, this is why accessing the common properties like tag works, but not the Building specific properties.

When I create body, I set into user data sprite.
In the Building.h
body->SetUserData(self);
solved the problem.

Related

Instantly remove AdMob banner?

I've made a game in using cocos2d and have integrated an admob banner in the appdelegate so that it is displayed on all view controllers. Ive implemented a remove ads feature which happens in the "game over" screen. Here is my problem. The remove ads function works but does not take affect immediately. I have to kill the app and restart it for the banner to go away. Is there a way to just reload the appdelegate and the ads are removed instantly instead of having to reload the app? PLEASE HELP This is driving me crazy.
EDITED: HERE IS MY CURRENT CODE. THE BANNER LOADS WHEN THE APP LOADS I AM TRYING TO MAKE THE BANNER GO AWAY WHEN THE USER PURCHASES TO REMOVE IT WITHOUT HAVING TO RESTART THE APP(CLOSE THE APP RESTART)
AppDelegate.h
#import "GADBannerViewDelegate.h"
// Added only for iOS 6 support
#interface MyNavigationController : UINavigationController <CCDirectorDelegate>
#end
#class RootViewController;
#class GADBannerView, GADRequest;
#interface AppController : NSObject <UIApplicationDelegate,ADBannerViewDelegate,UIActionSheetDelegate, GKLeaderboardViewControllerDelegate, GameCenterManagerDelegate,ChartboostDelegate,GADBannerViewDelegate>
{
UIWindow *window_;
MyNavigationController *navController_;
CCDirectorIOS *director_;
UIViewController *tempVC; // weak ref
RootViewController *viewController_;
//Admob
GADBannerView *bannerView_;
BOOL isAdPositionAtTop_;
}
AppDelegate.m i use calladmob to start ads if removeads is "no"
-(void)callAdMob{
//Admob ads
CGPoint origin = CGPointMake(0.0,self.window.frame.size.height -CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait origin:origin];
bannerView_.adUnitID = #"idhere";
bannerView_.delegate = self;
[bannerView_ setRootViewController:tempVC];
CGRect adBannerViewFrame = [bannerView_ frame];
int lSizeValue=0;
if([[CCDirector sharedDirector] winSize].width==320)
lSizeValue=50;
else if([[CCDirector sharedDirector] winSize].width==768)
lSizeValue=66;
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = [[CCDirector sharedDirector] winSize].height-lSizeValue;
[bannerView_ setFrame:adBannerViewFrame];
[[self navController].view addSubview:bannerView_];
[bannerView_ loadRequest:[self createRequest]];}
Why not send a notification from your "remove ads" function?
And then anywhere you have an AdMob banner, you can add an observer that watches for that notification and the method that gets called would do something like:
[bannerView removeFromSuperview] or bannerView.hidden = YES
provided you have your AdMob banner connected to an IBOutlet.

activate Button from added CCScene

I am adding a new layer to a scene. The new layer is loaded from a ccb file which includes buttons:
[[CCDirector sharedDirector] pause]; //pauses current scene
CCScene *pauseMenue = [CCBReader loadAsScene:#"Pause"];
pauseMenue.positionType = CCPositionTypeNormalized;
pauseMenue.position= ccp(0.5,0.5);
[self addChild:pauseMenue]; //adds the pause Layer
How do I get the buttons to work? Using the CCControl Selector doesn't seem to work…
-Pause.ccb doesn't consists of further ccb files.
-Pause.ccb has its custom class in which I tried to implement the selectors.

how to create menu using a sprite sheet in cocos2d-x

I just started learning cocos2d-x.
I am trying to create menu using sprite sheet, below:
CCSpriteFrameCache::sharedSpriteFrameCache()>addSpriteFramesWithFile("my_menu.plist");
CCMenuItem *play = CCMenuItemImage::create("play.png", NULL,NULL,this , menu_selector(StartScene::clickStart));
CCMenu *pMenu = CCMenu::create(play,NULL);
addChild(pMenu);
I got error message:
get data from file (play.png) failed.
I realise something wrong with my create function. I am just wondering how to get the image from sharedSpriteFrameCache?
OK, I just figure it out:
CCMenuItemSprite *play = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName("play.png"), NULL,NULL,this ,menu_selector(StartScene::clickStart));
spriteWithSpriteFrameName is deprecated, instead, we can use:
CCSprite::createWithSpriteFrameName();
u need to take the plist file in a ccspritebatchnode object and manipulate it using this object.
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"AnimBear.png"];
[self addChild:spriteSheet];

Object Order in CCLayer ( cocos2d-iphone )

I initialize my CCLayer using the following init code:
- (id)init {
if((self=[super init])) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.wantsFullScreenLayout = YES;
picker.allowsEditing = NO;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;
picker.cameraViewTransform= CGAffineTransformMakeScale(1.3, 1.33);
[[[CCDirector sharedDirector] openGLView] addSubview:picker.view];
CCSprite *gold = [CCSprite sprite];
gold.position = ccp(150, 150);
[self addChild:gold];
}
return self;
}
The CCSprite was above the camera view before the camera shutter opens, but when the shutter opens, the CCSprite is overlapped by the camera.
Can I rearrange the order of these 2 objects / put the camera view to the back ?
Not without some extra work.
To understand this you have to consider that you're adding the camera view to the openGLView by Cocos2D. This makes the camera view a "child" view of the Cocos2D view. This is similar to adding a child node to a CCScene or CCLayer, and then wanting to have that node drawn behind the scene or layer. You can't do this without changing the way the view hierarchy is setup.
So you will have to modify Cocos2D's startup so that the openGLView is not added to the UIWindow directly, but to another UIView.
UIView* dummyView = [[UIView alloc] initWithFrame:[window bounds]];
[dummyView autorelease];
[dummyView addSubview:[CCDirector sharedDirector].openGLView];
rootViewController.view = dummyView;
You can then add the camera view to the dummyView (not the openGLView or you'll have the same problem as before) and perform sendSubviewToBack on it so that it is in the background.
You will also have to initialize the OpenGL view of Cocos2D with kEAGLColorFormatRGBA8 pixelFormat in order to provide an alpha channel.
EAGLView* glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
You also need to make the openGLView transparent:
[CCDirector sharedDirector].openGLView.opaque = NO;
Of course this only works if you don't render anything fullscreen on the cocos2d view. For example, if you provide a fullscreen background image for your Cocos2D view, nothing will show up because the background image is rendered on top of the camera view.
You can find a more detailed explanation in the second edition of my book. You can also download the book's source code from that link and see the examples of chapter 15. Or download Kobold2D and use the provided Cocos2D-With-UIKit-Views template project.

Cocos2d menu programming *without* images

Good evening all,
I'm trying to code a menu, but I keep getting
Thread 1: Program received signal: "SIGABRT".
My code is minimal at the moment, just trying to get it to work!
#implementation Menu
+(id)scene{
CCScene *scene = [CCScene node];
CCLayer *layer = [Menu node];
[scene addChild:layer];
return scene;
}
-(id)init{
if((self = [super init])){
CCLabelTTF *playLabel = [CCLabelTTF labelWithString:#"Play" fontName:#"Marker Felt" fontSize:40];
CCMenuItemLabel *play = [CCMenuItemLabel itemWithLabel:playLabel target:self selector:#selector(doPlay:)]; //This is where SIGABRT happens//
menu = [CCMenu menuWithItems:play, nil];
[self addChild:menu];
}
return self;
}
-(void)doPlay{
CCLOG(#"doPLay");
}
#end
Any help would be greatly appreciated :)
There seems to be quite little on coding menus without images.
First order of business:
Go to Build Settings
Locate the compiler warning flag "Undeclared Selector"
Set it to YES
This will catch a lot of similar errors, and I really don't understand why this setting isn't turned on by default in all Xcode projects.
Let me explain what your error is, it's easy to overlook without that warning enabled. The selector passed to menu item is this:
#selector(doPlay:)
The selector that's implemented is this:
-(void) doPlay
{
}
They don't match! The menu item is expecting a selector that takes one parameter, as denoted by the : (colon). Change selector to this:
#selector(doPlay)
You'll be fine. Next time, the compiler will warn you about that mishap.