//
// MainScene.cpp
// helloworld
//
// Created by apple on 16/9/19.
//
//
#include "MainScene.hpp"
Scene * MainScene::createScene()
{
auto scene = Scene::create();
// CCScene * scene = CCScene::create();// 創建場景
//創建層
MainScene *layer = MainScene::create();
scene->addChild(layer);
return scene;
}
bool MainScene::init(){
if (!Layer::init()) {
return false;
}
//獲取屏幕大小
Size visibleSize = Director::getInstance()->getVisibleSize();
//創建精靈的六種方法
//方法一
auto sprite1 = Sprite::create("Flag_1.png");
sprite1->setPosition(Vec2(visibleSize.width*0.2,visibleSize.height*0.7));
this->addChild(sprite1);
//方法二
auto sprite2 = Sprite::create("Flag_1.png",Rect(0,0,48,36));
sprite2->setPosition(Vec2(visibleSize.width*0.4,visibleSize.height*0.7));
this->addChild(sprite2);
//創建2D紋理
auto texture1 = Director::getInstance()->getTextureCache()->addImage("Flag_1.png");
//auto texture1 = TextureCache::sharedTextureCache()->addImage("Flag_1.png"); //cocos2d-x 3.0已移除該方法
//方法三
auto sprite3 = Sprite::createWithTexture(texture1);
sprite3->setPosition(Vec2(visibleSize.width*0.6,visibleSize.height*0.7));
this->addChild(sprite3);
//方法四
auto sprite4 = Sprite::createWithTexture(texture1,Rect(0,0,48,36));
sprite4->setPosition(Vec2(visibleSize.width*0.8,visibleSize.height*0.7));
this->addChild(sprite4);
//創建精靈幀
auto frame = SpriteFrame::create("Flag_1.png",Rect(0,0,48,36));
//方法五
auto sprite5 = Sprite::createWithSpriteFrame(frame);
sprite5->setPosition(Vec2(visibleSize.width*0.3,visibleSize.height*0.3));
this->addChild(sprite5);
// //方法六
// SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("img.plist");
//
// auto sprite6 = Sprite::createWithSpriteFrameName("HelloWorld.png");
// sprite6->setPosition(Vec2(visibleSize.width*0.7, visibleSize.height*0.3));
// this->addChild(sprite6);
//**************************//
//精靈常用處理操作
//設置縮放
sprite1->setScale(1.5f,1.5f);
// sprite1->setScaleX(1.5f);
// sprite1->setScaleY(1.5f);
//設置旋轉,大於零順時針旋轉
sprite2->setRotation(60.0f);
// sprite2->setRotationX(60.0f);
// sprite2->setRotationY(60.0f);
//設置傾斜
sprite3->setSkewX(30.0f);
// sprite3->setSkewY(30.0f);
//設置是否可見
sprite4->setVisible(true);
//設置顏色
sprite1->setColor(Color3B(0,0,255));
//設置透明度
sprite2->setOpacity(100);
//相關屬性
auto sprite = Sprite::create("CloseSelected.png");//創建一個精靈
// Sprite * sprite = CCSprite::create("XX圖片");
sprite ->setPosition(50, 60);// 坐標(左下為00)
sprite->setAnchorPoint(Vec2(0, 0));//設置錨點為左下角,默認為 ccp(0.5,0.5)中心點
sprite ->setRotation(90);// 精靈圖片旋轉90度(當然也可以90.3f)浮點型
sprite ->setScale(2.0f);// //放大2倍,參數為比例,1保持不變,0.5代表50%,2代表200%精靈圖片放大1倍(長寬都是),正常是1,縮小1倍是0.5
sprite ->setScaleZ(100);// 相當於層級關系,當有兩個精靈重疊的時候,數值越大越靠前
sprite ->setOpacity(255);//設置透明度為完全不透明(范圍0~255)
sprite ->setFlippedX(false);//X軸鏡像反轉
sprite ->setFlippedY(false);//Y軸鏡像反轉
sprite ->setVisible(true);//設置隱藏,默認為可見
sprite ->setTextureRect(Rect(10, 10, 30, 30));//起始點坐標(做上角坐標系),寬高
// auto visibleSize = Director::getInstance()->getVisibleSize();
// Vec2 origin = Director::getInstance()->getVisibleOrigin();
// sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(sprite);// 添加到層
// 注冊回調,用一個宏來包裝一下(函數名不要有函數)
schedule(schedule_selector(MainScene::update));
// 貞回調可用設置選擇器與時間
schedule(schedule_selector(MainScene::update), 1);// 一秒調用一次
return true;
}
// 每秒調用60次
void MainScene::update(float t){
log("ok");
// 修改坐標點,讓它向右平移(首先要把精靈換成全局變量,因為這里需要)
auto sprite = Sprite::create("CloseSelected.png");
// 獲取到當前精靈的坐標,X軸+1,Y軸不加
sprite -> setPosition(Point(sprite->getPosition(), Vec2(1,0)));
// 獲取到當前的旋轉角度,調用時+1;
sprite -> setRotation(sprite->getRotation()+1);
}
