admin管理员组文章数量:1122875
【cocos2D
-
要点 : 制作图集要和背景图一起制作 plist文件 要放一个文件夹
-
原图片也是放在一个文件夹
-
-
这个代码 数字 与文件名对应 , 如果出现报错 多数就是数值 越界 考虑图片数减少
AppDelegate.h
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_#include "cocos2d.h"/**
@brief The cocos2d Application.Private inheritance here hides part of interface from Director.
*/
class AppDelegate : private cocos2d::Application
{
public:AppDelegate();virtual ~AppDelegate();virtual void initGLContextAttrs();/**@brief Implement Director and Scene init code here.@return true Initialize success, app continue.@return false Initialize failed, app terminate.*/virtual bool applicationDidFinishLaunching();/**@brief Called when the application moves to the background@param the pointer of the application*/virtual void applicationDidEnterBackground();/**@brief Called when the application reenters the foreground@param the pointer of the application*/virtual void applicationWillEnterForeground();
};#endif // _APP_DELEGATE_H_
helloworld.h
#include "cocos2d.h"
#include <vector>
using namespace std;
class MyHelloWorld: public cocos2d::Scene {
public:cocos2d::Vector<cocos2d::SpriteFrame*> getAnimation(const char* format, int count); //addstatic cocos2d::Scene* createScene();virtual bool init();void menuCloseCallback(cocos2d::Ref* pSender);CREATE_FUNC(MyHelloWorld);
};//cpp
/****************************************************************************Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.****************************************************************************/#include "MyHelloWorld.h"
#include <vector>
USING_NS_CC;Scene* MyHelloWorld::createScene()
{return MyHelloWorld::create();
}// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{printf("Error while loading: %s\n", filename);printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}Vector<SpriteFrame*> MyHelloWorld::getAnimation(const char* format, int count)
{auto spritecache = SpriteFrameCache::getInstance();Vector<SpriteFrame*> animFrames;char str[100];for (int i = 0; i <= count; i++){sprintf(str, format, i);animFrames.pushBack(spritecache->getSpriteFrameByName(str));}return animFrames;
}// on "init" you need to initialize your instance
bool MyHelloWorld::init()
{//// 1. super init firstif (!Scene::init()){return false;}auto visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();SpriteFrameCache::getInstance()->addSpriteFramesWithFile("tong.plist"); // 缓存粒子 auto bg = Sprite::create("bg1.jpg");bg->setScale(0.8);bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));this->addChild(bg);auto frames = getAnimation("BucketheadZombie/BucketheadZombie_%d.png", 14); // 调用加载图片auto sprite = Sprite::createWithSpriteFrame(frames.front());bg->addChild(sprite);sprite->setPosition(300, 300);auto animation1 = Animation::createWithSpriteFrames(frames, 1.0f / 13); // 缓存时间 约小则加载越快 sprite->runAction(RepeatForever::create(Animate::create(animation1)));return true;
}void MyHelloWorld::menuCloseCallback(Ref* pSender)
{//Close the cocos2d-x game scene and quit the applicationDirector::getInstance()->end();/*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() as given above,instead trigger a custom event created in RootViewController.mm as below*///EventCustom customEndEvent("game_scene_close_event");//_eventDispatcher->dispatchEvent(&customEndEvent);}
}
本文标签: Cocos2d
版权声明:本文标题:【cocos2D 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1687733380a134818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论