1 //2 //mainscene.hpp3 //HelloWorld4 //5 //Created by Apple on 16/9/19.6 //7 //8 9 #ifndef mainscene_hppTen #defineMainscene_hpp One A#include <stdio.h> -#include"cocos2d.h" - using_ns_cc; the using namespacecocos2d; - //defining a Scene class - - classMainscene: Publiccocos2d::layer{ + - Private: + //member variable (private) ASize size;//Note You cannot use the Auto keyword atSprite *sprite1, *sprite2;//A wizard that defines two member variables (note that you cannot use the Auto keyword -Label *label;//global variable label-> show collision and non-collision (note cannot use auto keyword) - Public: - Virtual BOOLInit ();//virtual function, return value is Boolean type, no function - - Staticcocos2d::scene* Createscene ();//Static is a class method that returns a scene in //declaring a tap gesture - Virtual BOOLOntouchbegan (Touch *touch, Event *unused_event); to //declaring a drag gesture + Virtual voidOntouchmoved (Touch *touch, Event *unused_event); - the Create_func (mainscene); * }; $ Panax Notoginseng - #endif/* MAINSCENE_HPP */
1 //2 //MainScene.cpp3 //HelloWorld4 //5 //Created by Apple on 16/9/19.6 //7 //8 9#include"mainscene.hpp"TenCocos2d::scene *Mainscene::createscene () One { AAuto scene =scene::create (); - //ccscene * scene = Ccscene::create ();//Create a scene - //Creating Layers theMainscene *layer =mainscene::create (); -Scene->addChild (layer); - returnscene; - } + BOOLMainscene::init () { - if(!Layer::init ()) { + return false; A } at //Get screen size -Size = Director::getinstance ()getvisiblesize (); - //Auto size = director::getinstance ()->getwinsize (); - //Add a picture sprite -Sprite1 = Sprite::create ("Closenormal.png"); -Sprite1->setposition (VEC2 (size.width/2, size.height/2)); in This-AddChild (sprite1); - toSprite2 = Sprite::create ("Closenormal.png"); +Sprite2->setposition (VEC2 (size.width*0.4, size.height/2)); - This-AddChild (sprite2); the *Label = Label::createwithsystemfont ("No Collision","Marker Felt", -); $Label->setposition (VEC2 (size.width/2, size.height*0.7));Panax Notoginseng This->addchild (label);//display the label directly on the screen - the //To create a listener event object +Auto Listener =eventlistenertouchonebyone::create (); A the //defining callback functions for listening events +Listener->ontouchbegan = Cc_callback_2 (Mainscene::ontouchbegan, This); -listener->ontouchmoved = Cc_callback_2 (mainscene::ontouchmoved, This); $ $ //registering in the event dispatcher -_eventdispatcher->addeventlistenerwithscenegraphpriority (Listener, This); - the return true; - }Wuyi the //Click the Move fine event callback method - BOOLMainscene::ontouchbegan (Touch *touch, Event *unused_event) Wu { - //get the touch screen position (coordinates) AboutPoint pos = touch->getLocation (); $ -Sprite2->setposition (POS);//Assign the touch Point (coordinates) of the tap screen to the Sprite 2 - -Log"Touchbegan"); A + return true; the } - //Drag Gesture Execution Event method $ voidMainscene::ontouchmoved (Touch *touch, Event *unused_event) the { the //gets the coordinates and position of the current drag gesture thePoint pos = touch->getLocation (); the -Sprite2->setposition (POS);//Let the genie move along the drag point in the //determine if two sprites intersect the if(Sprite2->getboundingbox (). Intersectsrect (Sprite1->getboundingbox ())) {//3.0 Change of place AboutLabel->setstring ("collision!");//change the text of a label on the screen the}Else{ theLabel->setstring ("No Collision");//change the text of a label on the screen the +Log"touchmoved"); - } the}
Cocos2dx 3.x (Ghost Collision detection, click Move and Drag Wizard)