《Cocos2d-x for iPhone游戏开发实例详解》系列博文,基于 人民邮电出版社 《Cocos2d for iPhone游戏开发实例详解》一书,使用Cocosd-x技术重写。示例代码中所引用的相关资料归于原书作者 Nathan Burba 以及出版社所有,本系列博文提供的C++版代码仅供学习使用。
======================================================================================
开发环境:
Cocos2d-x cocos2d-2.1rc0-x-2.1.2-hotfix.zip @ Apr.08, 2013
MacBook Pro 13' Mountain Lion 10.8.3
Xcode 4.6
iPhone5 IOS 6.1.4
======================================================================================
本章 代码相对于中文翻译版以及英文原版有很大出入,以英文原版Cocos2d.for.iPhone.1.Game.Development.Cookbook Coder Source 为基准修改。本示例展示了绘制运动条纹的方法。
//****************************************************************************************************
// Author: Last Ranker
// DateTime: 2013年08月24日
// SearchMe: http://blog.youkuaiyun.com/lastranker
// Email: tubufeng@foxmail.com
//
//****************************************************************************************************
// DrawingSprites.h
// Chapter_1
//
// Created by Last Ranker on 13-8-15.
//
//
//
// MotionStreak.h
// Chapter_1
//
// Created by Last Ranker on 13-8-24.
//
//
#ifndef __Chapter_1__MotionStreak__
#define __Chapter_1__MotionStreak__
#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
class MotionStreak: public cocos2d::CCLayer
{
public:
CCPoint rocketDirection;
CCSprite *rocket;
CCMotionStreak *streak;
virtual bool init();
static cocos2d::CCScene * scene();
CREATE_FUNC(MotionStreak);
CCLayer * runRecipe();
void step(CCTime delta);
static float VectorToRadians(CCPoint vector);
float RadiansToDegrees(float radians);
};
#endif /* defined(__Chapter_1__MotionStreak__) */
//****************************************************************************************************
// Author: Last Ranker
// DateTime: 2013年08月24日
// SearchMe: http://blog.youkuaiyun.com/lastranker
// Email: tubufeng@foxmail.com
//
//****************************************************************************************************
//
// MotionStreak.cpp
// Chapter_1
//
// Created by Last Ranker on 13-8-24.
//
//
#include "MotionStreak.h"
#include <math.h>
CCScene * MotionStreak::scene()
{
CCScene *scene=CCScene::create();
MotionStreak *layer=MotionStreak::create();
scene->addChild(layer);
return scene;
}
bool MotionStreak::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
runRecipe();
return true;
}
CCLayer * MotionStreak::runRecipe()
{
CCSize s=CCDirector::sharedDirector()->getWinSize();
//Set the rocket initially in a random direction.
rocketDirection=ccp(arc4random()%4+1, arc4random()%4+1);
//Add the rocket sprite.
rocket=CCSprite::create("rocket.png");
rocket->setPosition(ccp(s.width/2, s.height/2));
rocket->setScale(0.5f);
this->addChild(rocket);
//Create the streak object and add it to the scene.
streak=CCMotionStreak::create(1, 1, 32, ccc3(255, 255, 255), "streak.png");
this->addChild(streak);
streak->setPosition(ccp(s.width/2, s.height/2));
//this->schedule(schedule_selector(MotionStreak::step),0.0000001);
this->schedule(schedule_selector(MotionStreak::step));
return this;
}
void MotionStreak::step(CCTime delta)
{
CCSize s=CCDirector::sharedDirector()->getWinSize();
//Make rocket bounce off walls
if (rocket->getPosition().x>s.width||rocket->getPosition().x<0) {
rocketDirection=ccp(-rocketDirection.x, rocketDirection.y);
}
else if(rocket->getPosition().y>s.width||rocket->getPosition().y<0)
{
rocketDirection=ccp(rocketDirection.x, -rocketDirection.y);
}
//Slowly turn the rocket
rocketDirection = ccp(rocketDirection.x, rocketDirection.y+0.05f);
//Update rocket position based on direction
rocket->setPosition(ccp(rocket->getPosition().x+rocketDirection.x,rocket->getPosition().y+rocketDirection.y));
streak->setPosition(rocket->getPosition());
//Set the rocket's rotation
rocket->setRotation(RadiansToDegrees(VectorToRadians(rocketDirection)));
}
float MotionStreak::VectorToRadians(CCPoint vector)
{
return (float)atan2(vector.x,-vector.y);
}
float MotionStreak::RadiansToDegrees(float radians)
{
return radians*180/M_PI;
}
源代码:http://pan.baidu.com/share/link?shareid=287519970&uk=1962963338