码迷,mamicode.com
首页 > 其他好文 > 详细

编写MyLayer,2 锚点,3 精灵的创建,4 zorder

时间:2017-06-28 21:48:14      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:round   space   ati   file   delete   div   方法   bsp   bullet   

??

1 编写MyLayer

头文件:MyLayer.h

#include "cocos2d.h"

USING_NS_CC;   //代表的是: using namespace cocos2d   (通过F12进行查看)

 

//继承CCLayer

class MyLayer :public CCLayer

{

public:

    //没有构造函数和析构函数

    CREATE_FUNC(MyLayer);

    /*static MyLayer *create();*/

 

    static CCScene * scene();

    bool init();

};

MyLayer.cpp

#include "MyLayer.h"

 

//MyLayer *MyLayer::create(){

//  MyLayer * pRet = new MyLayer();

//  if (pRet && pRet->init())

//  {

//      pRet->autorelease();

//  }

//  else

//  {

//      delete pRet;

//      pRet = NULL;

//  }

//  return pRet;

//}

 

bool MyLayer::init()

{

    if (!CCLayer::init())

    {

        return false;

    }

 

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

   

    //创建一个精灵

    CCSprite * spr = CCSprite::create("anchor3.png");

    spr->setPosition(ccp(winSize.width/2,winSize.height /2));

    //加入一个精灵

    addChild(spr);

 

    return true;

}

 

CCScene *MyLayer::scene()

{

    CCScene * scene = CCScene::create();

    MyLayer * layer = MyLayer::create();

    scene->addChild(layer);

    return scene;

}

要想让写的Layer可以执行,须要在AppDelegate.app中加入例如以下代码

1、 加入头文件:#include "MyLayer.h"

2、 bool AppDelegate::applicationDidFinishLaunching() 改动例如以下内容:

 

bool AppDelegate::applicationDidFinishLaunching() {

    // initialize director

    CCDirector* pDirector = CCDirector::sharedDirector();

    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

 

    pDirector->setOpenGLView(pEGLView);

   

    // turn on display FPS

    pDirector->setDisplayStats(true);

 

    // set FPS. the default value is 1.0/60 if you don‘t call this

    pDirector->setAnimationInterval(1.0 / 60);

 

    // create a scene. it‘s an autorelease object

    //CCScene *pScene = HelloWorld::scene();

    CCScene * pScene = MyLayer::scene();      

 

    // run

    pDirector->runWithScene(pScene);

 

    return true;

}

执行结果:

技术分享

2 锚点

LayerAnchorPoint.h

#ifndef __T01LayerAnchorPoint_H__

#define __T01LayerAnchorPoint_H__

 

#include "cocos2d.h"

USING_NS_CC;

 

class LayerAnchorPoint:public CCLayer

{

public:

    static LayerAnchorPoint * create();

    bool init();

 

    static CCScene * scene();

 

    void draw();

 

    void mySchedule(float dt);

    CCSprite *spr;

};

 

#endif

LayerAnchorPoint.cpp

#include "LayerAnchorPoint.h"

 

CCScene * LayerAnchorPoint::scene()

{

    //创建一个场景

    CCScene * scene = CCScene::create();

    LayerAnchorPoint * layer = LayerAnchorPoint::create();

    scene->addChild(layer);

    return scene;

}

 

LayerAnchorPoint * LayerAnchorPoint::create() {

    LayerAnchorPoint *pRet = new LayerAnchorPoint();

    if (pRet && pRet->init())

    {

        pRet->autorelease();

    }

    else

    {

        delete pRet;

        pRet = NULL;

    }

    return pRet;

}

 

bool LayerAnchorPoint::init()

{

    CCLayer::init();

   

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    spr = CCSprite::create("anchor1.png");

    //设置锚点

    //spr->setAnchorPoint(ccp(0.656,0.5));

    spr->setScale(5.0f);

    spr->setPosition(ccp(winSize.width/2,winSize.height/2));

    addChild(spr);

 

    //设置一个定时器,频率是2

    schedule(schedule_selector(LayerAnchorPoint::mySchedule), 2);

    return true;

}

 

//通过以下的方法实现画线

void LayerAnchorPoint::draw()

{

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    //RGB颜色。当中最后一个參数是透明率

    ccDrawColor4B(255,0,0,255);

 

    ccDrawLine(ccp(0, winSize.height / 2), ccp(winSize.width, winSize.height / 2));

    ccDrawLine(ccp(winSize.width / 2, 0),ccp(winSize.width/2,winSize.height));

}

 

//通过以下的方式设置旋转

void LayerAnchorPoint::mySchedule(float dt) {

    static float ro = 0;

    ro += 30;

    spr->setRotation(ro);

}

改动:AppDelegate.cpp中的代码

头文件:

#include "LayerAnchorPoint.h"

 

bool AppDelegate::applicationDidFinishLaunching() {

    // initialize director

    CCDirector* pDirector = CCDirector::sharedDirector();

    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

 

    pDirector->setOpenGLView(pEGLView);

   

    // turn on display FPS

    pDirector->setDisplayStats(true);

 

    // set FPS. the default value is 1.0/60 if you don‘t call this

    pDirector->setAnimationInterval(1.0 / 60);

 

    // create a scene. it‘s an autorelease object

    //CCScene *pScene = HelloWorld::scene();

    //CCScene * pScene = MyLayer::scene();

    CCScene * pScene = LayerAnchorPoint::scene();

 

    // run

    pDirector->runWithScene(pScene);

 

    return true;

}

执行结果:

技术分享

技术分享

3 精灵的创建

1、编写:LayerSprite.h

#include "cocos2d.h"

 

USING_NS_CC;

class LayerSprite:public CCLayer

{

public:

    static CCScene * scene();

    CREATE_FUNC(LayerSprite);

    bool init();

 

};

编写:表示屏幕大小的头文件AppMacros.h

#ifndef __APPMACROS_H__

#define __APPMACROS_H__

 

#include "cocos2d.h"

 

//通过提前定义获得屏幕大小

#define  winSize  CCDirector::sharedDirector()->getWinSize()

 

#endif /* __APPMACROS_H__ */

编写:LayerSprite.cpp

#include "LayerSprite.h"

#include "AppMacros.h"

 

 

CCScene *LayerSprite::scene()

{

    CCScene * scene = CCScene::create();

    LayerSprite * layer = LayerSprite::create();

    scene->addChild(layer);

    return scene;

}

bool T02LayerSprite::init()

{

    CCLayer::init();

 

    CCSprite * bg = CCSprite::create("sprite/background.jpg",CCRectMake(0,0,480,320));

    bg->setAnchorPoint(ccp(0,0));

    bg->setPosition(CCPointZero);

    addChild(bg);

 

    CCImage * image = new CCImage();

    image->initWithImageFile("sprite/plant.png");

 

    CCTexture2D * texture = new CCTexture2D();

    texture->autorelease();

    texture->initWithImage(image);

    CCSprite * plant = CCSprite::createWithTexture(texture); //纹理

    plant->setPosition(ccp(100,winSize.height/2));

    addChild(plant);

 

    CCTexture2D * texture2 = CCTextureCache::sharedTextureCache()->addImage("sprite/bullet.png");

    CCSprite * bullet = CCSprite::createWithTexture(texture2);

    bullet->setPosition(ccp(200, winSize.height / 2+10));

    addChild(bullet);

 

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite/zombie.plist");

    //CCSpriteFrame *sf = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("z_1_attack_01.png");

    //CCSprite *zombie = CCSprite::createWithSpriteFrame(sf);

    CCSprite *zombie = CCSprite::createWithSpriteFrameName("z_1_attack_01.png");

    zombie->setPosition(ccp(400, winSize.height / 2-50));

    addChild(zombie);

 

    CCJumpBy * by = CCJumpBy::create(2, ccp(0,0),40,3);

    zombie->runAction(CCRepeatForever::create(by));

 

    CCSpriteFrame * frame = CCSpriteFrame::create("sprite/p_1_01.png", CCRectMake(0, 0, 80, 80));

    CCSprite * sun = CCSprite::createWithSpriteFrame(frame);

    sun->setPosition(ccp(400, winSize.height / 2 + 50));

    addChild(sun);

 

    return true;

}

同上,将LayerSprite.h加到AppDelegate.cpp中,并加入上:

CCScene * pScene = LayerSprite::scene();

执行结果:

技术分享

4 zorder

LayerZorder.h

#include "cocos2d.h"

USING_NS_CC;

 

class LayerZorder:public CCLayer

{

public:

    static CCScene * scene();

    CREATE_FUNC(LayerZorder);

    bool init();

};

LayerZorder.cpp

#include "LayerZorder.h"

#include "AppMacros.h"

 

CCScene *LayerZorder::scene()

{

    CCScene * scene = CCScene::create();

    LayerZorder * layer = LayerZorder::create();

    scene->addChild(layer);

    return scene;

}

 

bool LayerZorder::init()

{

    CCLayer::init();

 

    CCSprite * man = CCSprite::create("zorder/car42.png");

    //将图片缩放

    man->setScale(0.3f);

    CCSprite * woman = CCSprite::create("zorder/car43.png");

    woman->setScale(0.3f);

    CCSprite * smallThree = CCSprite::create("zorder/car51.png");

    smallThree->setScale(0.3f);

 

    man->setPosition(ccp(winSize.width / 2 + 40, winSize.height / 2 - 30));

    smallThree->setPosition(ccp(winSize.width / 2, winSize.height / 2 + 30));

    woman->setPosition(ccp(winSize.width / 2 - 40, winSize.height / 2 + 30));

 

    addChild(man);

    addChild(smallThree);

    addChild(woman);

 

    //通过setZOrder来设置图片显示顺序

    man->setZOrder(100);

    smallThree->setZOrder(50);

    woman->setZOrder(0);

    return true;

 

    /*CCLog("%d", man->getZOrder());

    CCLog("%d", smallThree->getZOrder());

    CCLog("%d", woman->getZOrder());*/

    return true;

}

改动AppDelegate.cpp

1.  zorder的头文件进入:#include "LayerZorder.h"

2.  CCScene * pScene = LayerZorder::scene();

执行结果:

技术分享

 

编写MyLayer,2 锚点,3 精灵的创建,4 zorder

标签:round   space   ati   file   delete   div   方法   bsp   bullet   

原文地址:http://www.cnblogs.com/slgkaifa/p/7091458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!