Cocos2d Getting started--3--ball sport

Source: Internet
Author: User

This chapter is directly on the source code. The content is not difficult, mainly is

HelloWorldScene.h file:

1 #ifndef __helloworld_scene_h__2 #define__helloworld_scene_h__3 4#include"cocos2d.h"5 6 classHelloWorld: PublicCocos2d::layer7 {8 protected:9     float_angle;Ten cocos2d::vec2 _vec; One  Public: A     Staticcocos2d::scene*Createscene (); -  -     Virtual BOOLinit (); the      -     //a selector callback -     voidMenuclosecallback (cocos2d::ref*psender); -      +     //implement the "Static Create ()" Method manually - Create_func (HelloWorld); +      A     Virtual voidUpdatefloatDT); at Private: -     //Get the screen visual range -     floatwidth_l; -     floatWidth_r; -Cocos2d::D rawnode*Ball ; - }; in  - #endif //__helloworld_scene_h__

HelloWorldScene.cpp file:

1 //On "Init" need to initialize your instance2 BOOLHelloworld::init ()3 {4     //////////////////////////////5     //1. Super init First6     if( !layer::init ())7     {8         return false;9     }Ten      OneSize visiblesize = Director::getinstance ()getvisiblesize (); AVEC2 origin = Director::getinstance ()Getvisibleorigin (); -width_l =origin.x; -Width_r = origin.x +Visiblesize.width; the      -Ball =drawnode::create (); -Ball--Drawdot (VEC2 (0,0),4, color4f (1.0f,1.0f,1.0f,1.0f)); -      + addChild (ball); -Ball--SetPosition (origin.x + visiblesize.width/2, ORIGIN.Y + visiblesize.height/2); +      A     //action-related motion we are generally not used to do game games, generally used to make the game change effect. Because the action is not very good to show the effect of the game at //dot-runaction (repeatforever::create (Moveby::create (0.2, _vec*100)); -     // - scheduleupdate (); -     return true; - } -  in voidHelloworld::update (floatDT) { -Ball--Setpositionx (Ball->getpositionx () +5); to     if(Ball->getpositionx () <width_l-ball->getcontentsize (). width/2 +|| Ball->getpositionx () >width_r+ball->getcontentsize (). width/2) { -Ball->setpositionx (-ball->getcontentsize (). width/2); the     } *}

Then achieve the effect:

Cocos2d Getting started--3--ball sport

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.