View iPhone app ads in cocos2d, c++ and objective-c

Asked 2 years ago, Updated 2 years ago, 70 views

cocos2d-x, based on c++
I'm studying to create an iPhone game app where leaflet advertisements are displayed at the bottom of the screen for free.

Currently, we are using setContentScaleFactor to change the magnification of expansion and contraction depending on the screen size of the terminal.Then, I created a class called MainScene, and it went well enough to show the first screen (image).

Next, I was going to put a frame on the screen to display lines, but I thought I had to implement it first because I needed to think about advertising space, so I moved on to the iAd deployment process.

I don't know how to deploy iAd in c++ (or is it impossible to implement?), so I created a new class called ViewController (extended in mm) to see if I could somehow display flyers in Objective-c, and wrote down the necessary actions.(viewDidLoad, DidReceiveMemoryWarning, viewDidAppear, bannerViewDidLoadAd, bannerView)

So I started the simulator, but the background image set in c++ is displayed, but the flyer is not displayed.The MainScene header is connected with #include "ViewController.h".

Do I have to call the ViewController of the flyer class from the main MainScene and call each method?In that case, the method of the Objective-c class will be called from the C++ class, and I don't know how to fill it out, so please let me know.
If C++ can implement iAd, that would be best.

*Games will mainly be created in C++ (simple game with just a touch of the image)
*X-code, creating with cocos2d-x
* I want iAd to be displayed at the bottom of the screen
* ↑ is created in Objective-c.
*Only the first C++ screen is displayed

I also want the iAd ad to be displayed in the app that I create with c++.I would like to know how to do this, and I really don't know (viewDidLoad, DidReceiveMemoryWarning, viewDidAppear, bannerViewDidLoadAd, bannerView) so I would like to know if this is all right.

Below is the ViewController source code ***
ViewController.h

#import<UIKit/UIKit.h>
# import<iAd/iAd.h>

@ interface ViewController:UIViewController <ADBannerViewDelegate >
@property(unsafe_unretained, nonatomic)IBOutlet ADBannerView*bannerView;
@end

Here's the source code for ViewController.mm.***

#import "ViewController.h"
@ interface ViewController()
@end

@implementation ViewController

- (void) viewDidLoad{
    superviewDidLoad;
}

- (void) DidReceiveMemoryWarning {
    super didReceiveMemoryWarning;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear: animated ];
    CGRect bannerFrame =self.bannerView.frame;
    bannerFrame.origin.y =self.view.frame.size.height;
    self.bannerView.frame =bannerFrame;
}

-(void)bannerViewDidLoadAd:(ADBannerView*)banner{
    CGRect bannerFrame = banner.frame;
    bannerFrame.origin.y =self.view.frame.size.height-banner.frame.size.height;
    UIView animateWithDuration: 1.0 animations:^{
    banner.frame = bannerFrame;
    }];
    NSLog (@ "Advertising In Stock");}

- (void) bannerView: (ADBannerView*) banner didFailToReceiveAdWithError: (NSError*) error {
    CGRect bannerFrame = banner.frame;
    bannerFrame.origin.y =self.view.frame.size.height;
    UIView animateWithDuration: 1.0 animations:^{
    banner.frame = bannerFrame;
    }];

    NSLog (@ "No Ad Stock");}

@end

This is where MainScene comes from.

MainScene.h

#ifndef__aa_MainScene__
#define__aaa__MainScene__

# include "cocos2d.h"
# include "ViewController.h"

class MainScene:public cocos2d:Layer{

protected:
    MainScene();
    virtual~MainScene();
    bool init() override;

public:
    static cocos2d::Scene*createScene();
    CREATE_FUNC (MainScene);

};

# endif / *defined(_aaa__MainScene__)*/

-----------------------------
MainScene.mm

#include "MainScene.h"

USING_NS_CC;

MainScene::MainScene(){


}

MainScene::~MainScene(){


}

Scene*MainScene::createScene(){//Methods for generating your own instance

    Autoscene=Scene::create(); // Generate yourself (MainScene)
    Auto layer=MainScene::create(); // Paste back to screen
    scene->addChild(layer);// Create an empty layer
    return scene;

}

US>bool MainScene::init(){

    if(!Layer::init()) {// In the initialization process for this class, true if initialization succeeds, false if it fails

        return false;

    }

    // Remove Director
    auto director=Director::getInstance();

    // Take out the screen size
    auto size=director->getWinSize();

    // Generate background sprites (first image)
    auto background=Sprite::create("aaa.png");

    // Set sprite display position (set to center)
    background->setPosition (Vec2(size.width/2.0, size.height/2.0));

    // Add sprite to parent node
    This ->addChild(background);


    // initialization process
    return true;
}

objective-c c++ iad

2022-09-29 22:13

1 Answers

I have never used cocos2d-x.Whether it's iAd or adMob, it's a special UIView for iOS.Therefore, if cocos2d-x can display UI/View of UI/Kit, all you have to do is write iAd or adMob settings and delete, and each library will do the rest.

Please refer to the relevant questions for your family.


2022-09-29 22:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.