setContentSize不cocos2dx3.0工作 [英] setContentSize is not working in cocos2dx3.0

查看:866
本文介绍了setContentSize不cocos2dx3.0工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

setContentSize不能正常工作

setContentSize is not working

我在cocos2dx新的,我想从另一个类添加图层我的主要SplashScreen类,但setContentSize使用设备的宽度和高度。

i am new in cocos2dx and i want to add layer from another class to my main Splashscreen class but setContentSize use device width and height.

我想用我的精灵大小设置以及大小层

i want to set layer size along with my sprite size.

看我的code

Gadget.cpp

Gadget.cpp

#include "Gadget.h"
#include "math.h"

int nCount = 100;
static CCPoint Circle [100]; // vertex array

Gadget* Gadget::create(int type)
{
    Gadget *pRet = new Gadget();
    if (pRet && pRet->init(type))
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

bool Gadget::init(int type)
{
    if(!LayerColor::init())
    {
        return false;
    }

    this->setContentSize(CCSize::ZERO);

    this->initWithColor(Color4B(255,255,255,255));

    CCLog("Before Calling");
    addGadget(type);

    return true;
}

Gadget::Gadget() {
    // TODO Auto-generated constructor stub

}

Gadget::~Gadget() {
    // TODO Auto-generated destructor stub
}

void Gadget::addGadget(int type)
{
    const char* image   =       "gadget_2.png";

    if(type==2)
    {
        image           =       "gadget_1.png";
    }
    //this->initWithFile(image);
    CCLog("After Calling");
    CCSprite *spr = CCSprite::create(image);
    setGadgetPos(this->getContentSize().width/2,0.0);
    this->drawCircle(100,100,100);

}

void Gadget::setGadgetPos(float x, float y)
{
//  this->ignoreAnchorPointForPosition(true);
    this->setPosition(x,y);
}

void Gadget::gadgetAnimStart(int type)
{
    if(type==1)
    {
        this->setAnchorPoint(ccp(0,3));
        auto rotate = RotateBy::create(3,360);
        this->runAction(CCRepeatForever::create(rotate));
    }
}

void Gadget::gadgetAnimStop()
{
    this->stopAllActions();
}

void Gadget::drawCircle(float X, float Y,float rad)
{
    DrawNode *drawnode = DrawNode::create();
    for ( int i = 0 ; i <100; i ++)
    {
        float rads = i * M_1_PI; // radians
        Circle [i] .x = rad * cosf (rads); //vertex x
        Circle [i] .y = rad * sinf (rads); //vertex y
    }

    drawnode->setPosition(Director::sharedDirector()->getVisibleSize().width/2,Director::sharedDirector()->getVisibleSize().height/2);
    drawnode->drawPolygon(Circle,100,Color4F(0,0,0,0),1,Color4F(1,122,153,1));
    this->addChild(drawnode);
}

我必须使用Gadget.coo类在Splashscreen.cpp

i have to use Gadget.coo class at Splashscreen.cpp

#include "Splashscreen.h"
#include "HelloWorldScene.h"
#include "CCHelper.h"
#include "Gadget.h"
#include "UserDefaultStorage.h"
#include "VisibleRect.h"
#include "math.h"
#include "Sound.h"
#include "Text_En.h"

USING_NS_CC;

Scene* Splashscreen::createScene()
{
    Scene *s = Scene::create();
    LayerColor *lc = Splashscreen::create();

    s->addChild(lc);
    return s;
}

Splashscreen::Splashscreen() {
    // TODO Auto-generated constructor stub
}

Splashscreen::~Splashscreen() {
    // TODO Auto-generated destructor stub
}

bool Splashscreen::init()
{
    if(!LayerColor::create())
    {
        return false;
    }


    this->initWithColor(ccc4(0,0,0,255));
    this->setContentSize(CCSizeZero);

    Gadget *g = Gadget::create(GREEN_GADGET);
    g->setPosition(ccp(-100,-100));
    this->addChild(g);

 return true;
}

当我使用

这 - > setContentSize(CCSizeZero); code,但鉴于没有变化。
其从-100位置产生全屏层,-100

when i use "this->setContentSize(CCSizeZero);" code but no changes in view. its generate fullscreen layer from position -100,-100

请给我一些解决方案,为什么setContentSize是行不通的。

please give me some solutions why setContentSize is not working.

在先进的感谢
RISHABH沙阿

Thanks in advanced Rishabh Shah

推荐答案

初始化后 setContentSize 将设置大小,但不会做出任何明显的变化,
更好的你在初始化提供的宽度和高度。

After initialization setContentSize will set size but won't make any visible change, better you provide width and height at initialization.

这样的:

 this->initWithColor(ccc4(0,0,0,255), width, height);

这篇关于setContentSize不cocos2dx3.0工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆