Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS大作业提交 #298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"engine_type": "prebuilt",
"engine_version": "cocos2d-x-3.8.1",
"project_type": "cpp"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::createWithRect("My game", Rect(0, 0, 320, 480));
director->setOpenGLView(glview);
//glview->setFrameSize(320, 480);
}

// turn on display FPS
director->setDisplayStats(true);

//���÷ֱ��ʴ�С
auto a = Size(320, 480);

//������Դ��С
auto b = Size(640, 960);

//���ó�����С
director->setContentScaleFactor(b.width/a.width);

//�涨��Ļ��Ӧ����
glview->setDesignResolutionSize(a.width, a.height, ResolutionPolicy::FIXED_WIDTH);




std::vector<std::string> searchPaths;
searchPaths.push_back("hd");

auto searchPath = FileUtils::getInstance();
searchPath->setSearchPaths(searchPaths);







// 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();

// run
director->runWithScene(scene);

return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will 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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_

#include "cocos2d.h"

/**
@brief The cocos2d Application.

The reason for implement as private inheritance is to hide some interface call by Director.
*/
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();

/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();

/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();

/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};

#endif // _APP_DELEGATE_H_

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "BaseLayer.h"


USING_NS_CC;

// on "init" you need to initialize your instance
bool BaseLayer::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

auto bg = TMXTiledMap::create("map/red_bg.tmx");
addChild(bg);

//眼镜精灵.
auto glassesSprite = Sprite::createWithSpriteFrameName("setting.glasses.png");
glassesSprite->setPosition(Vec2(visibleSize.width - glassesSprite->getContentSize().width /2, 160));
addChild(glassesSprite);

//手套精灵.
auto handSprite = Sprite::createWithSpriteFrameName("setting.hand.png");
handSprite->setPosition(Vec2(handSprite->getContentSize().width /2,200));
addChild(handSprite);

//Ok菜单.
auto okNormal = Sprite::createWithSpriteFrameName("setting.button.ok.png");
auto okSelected = Sprite::createWithSpriteFrameName("setting.button.ok-on.png");

auto okMenuItem = MenuItemSprite::create(okNormal, okSelected, CC_CALLBACK_1(BaseLayer::menuBackCallback, this));
auto mu = Menu::create(okMenuItem, NULL);
mu->setPosition(Vec2(visibleSize.width - mu->getContentSize().width /2 + 60, 60));
addChild(mu);


return true;
}


void BaseLayer::menuBackCallback(Ref* pSender)
{
Director::getInstance()->popScene();
if (UserDefault::getInstance()->getBoolForKey(SOUND_KEY)) {
SimpleAudioEngine::getInstance()->playEffect(sound_1);
}
}


void BaseLayer::onEnterTransitionDidFinish()
{
Layer::onEnterTransitionDidFinish();
log("BaseLayer onEnterTransitionDidFinish");
UserDefault *defaults = UserDefault::getInstance();
if (defaults->getBoolForKey(MUSIC_KEY)) {
SimpleAudioEngine::getInstance()->playBackgroundMusic(bg_music_1, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __BSAE_LAYER_H__
#define __BSAE_LAYER_H__

#include "cocos2d.h"

#include "SystemHeader.h"

class BaseLayer : public cocos2d::Layer
{
public:

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

virtual void onEnterTransitionDidFinish();

void menuBackCallback(cocos2d::Ref* pSender);

};

#endif // __BSAE_LAYER_H__
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "Bullet.h"

USING_NS_CC;

Bullet* Bullet::createWithSpriteFrameName(const char* spriteFrameName)
{
Bullet *bullet = new Bullet();
if (bullet && bullet->initWithSpriteFrameName(spriteFrameName)) {
bullet->autorelease();
bullet->setVisible(false);

auto body = PhysicsBody::createBox(bullet->getContentSize());

body->setCategoryBitmask(0x01); //0001
body->setCollisionBitmask(0x02); //0010
body->setContactTestBitmask(0x01);

bullet->setPhysicsBody(body);

return bullet;
}
CC_SAFE_DELETE(bullet);
return nullptr;
}

void Bullet::shootBulletFromFighter(Fighter* fighter)
{
this->setPosition(fighter->getPosition() + Vec2(0, fighter->getContentSize().height / 2));
this->setVisible(true);
this->unscheduleUpdate();
this->scheduleUpdate();

}

void Bullet::update(float dt)
{
Size screenSize = Director::getInstance()->getVisibleSize();
this->setPosition(Vec2(this->getPosition() + velocity *dt));
if (this->getPosition().y > screenSize.height) {
//log("isVisible = %d",this->isVisible());
this->setVisible(false);
this->unscheduleUpdate();
this->removeFromParent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef __BULLET_H__
#define __BULLET_H__

#include "cocos2d.h"
#include "Fighter.h"



class Bullet : public cocos2d::Sprite
{
CC_SYNTHESIZE(cocos2d::Vec2, velocity, Velocity);

public:

void shootBulletFromFighter(Fighter* fighter);

virtual void update(float dt);
static Bullet* createWithSpriteFrameName(const char* spriteFrameName);
};













#endif // __BULLET_H__
Loading