Cocos2d-x 3.0 Create a scene and set the scene when the project starts to execute on the active

Source: Internet
Author: User
Tags addchild

Head

#ifndef __test_h__
#define __test_h__


#include "Cocos2d.h"
USING_NS_CC;


Class Test:public Layer
{
Public://Do not forget the public or the default is that private is not normal execution
Static Scene * Createscene ();
virtual BOOL init ();
Create_func (Test);
};
#endif//__test_h__


source file

#include "Test.h"
USING_NS_CC;


Scene * Test::createscene ()
{
Auto scene = Scene::create ();
Auto layer = Test::create ();
Scene->addchild (layer);
return scene;
}
BOOL Test::init ()
{
if (! Layer::init ())
{
return false;
}


Size visiblesize = director::getinstance ()->getvisiblesize ();
VEC2 origin = Director::getinstance ()->getvisibleorigin ();


Auto label = Labelttf::create ("Hello World", "Arial", 24);
Label->setposition (VEC2 (origin.x + visiblesize.width/2,origin.y + visiblesize.height-label->getContentSize () . height));
This->addchild (label, 1);
return true;
}

And then in the AppDelegate.cpp. Add header File


#include "AppDelegate.h"
#include "HelloWorldScene.h"


#include "Test.h"//Add header file


USING_NS_CC;


Appdelegate::appdelegate () {


}


Appdelegate::~appdelegate ()
{
}


BOOL Appdelegate::applicationdidfinishlaunching () {
Initialize Director
Auto director = Director::getinstance ();
Auto Glview = Director->getopenglview ();
if (!glview) {
Glview = Glview::create ("My Game");
Director->setopenglview (Glview);
}


Turn on display FPS
Director->setdisplaystats (TRUE);


Set FPS. The default value is 1.0/60 if you don ' t call this
Director->setanimationinterval (1.0/60);


Create a scene. It ' s an Autorelease object
Auto scene = Helloworld::createscene ();
Auto scene = Test::createscene (); Instantiating a scene


Run
director->runwithscene (scene);//Add the instantiated scene name to it.


return true;
}


This function was called when the app was inactive. When comes a phone call,it ' s be invoked too
void Appdelegate::applicationdidenterbackground () {
Director::getinstance ()->stopanimation ();


If you use Simpleaudioengine, it must is pause
Simpleaudioengine::getinstance ()->pausebackgroundmusic ();
}


This function would be called when the app is active again
void Appdelegate::applicationwillenterforeground () {
Director::getinstance ()->startanimation ();


If you use Simpleaudioengine, it must resume here
Simpleaudioengine::getinstance ()->resumebackgroundmusic ();
}


























Cocos2d-x 3.0 Create a scene and set the scene when the project starts to execute on the active

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.