-
hamletLikes 0Problem Description
Hello Sonar , i have a problem with how to make a instantiation with one vector as member of one class please check the code as example :
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Pulsaciones.h"
#include <string>
#include "extensions/cocos-ext.h"
#include "ui/CocosGui.h"
#include <vector>
#include <algorithm>
#include <cassert>
#include <array>
#include <list>
#include <iostream>
using namespace cocos2d;
using namespace ui;
using std::copy;
using std::cout;
using std::endl;
using std::vector;
using namespace std;
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();virtual bool init();
// a selector callback
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
static int i_count;
static int u_count;
static int j_count;
static int k_count;
static int r_count;
int contador();
int contador1();
static int Charge;
Vec2 first,second,third,fourth,fifth,sixth;
Vec2 uno,dos,tres,cuatro,cinco;
Sprite *frontdisplay;
void setCode(Vec2,Vec2,Vec2,Vec2,Vec2/*,Vec2*/);
Vec2 getCode(int);
void setCode1(Vec2,Vec2,Vec2,Vec2,Vec2/*,Vec2*/);
Vec2 getCode1(int);
void Button1touchEvent( Ref *sender, Widget::TouchEventType type );
void Button2touchEvent( Ref *sender, Widget::TouchEventType type );
void Button3touchEvent( Ref *sender, Widget::TouchEventType type );
void Button4touchEvent( Ref *sender, Widget::TouchEventType type );
void Button5touchEvent( Ref *sender, Widget::TouchEventType type );
void Button6touchEvent( Ref *sender, Widget::TouchEventType type );
void ButtonDeletetouchEvent( Ref *sender, Widget::TouchEventType type );
void ButtonEntertouchEvent( Ref *sender, Widget::TouchEventType type );
void updatecode();
Pulsaciones* Button1,Button2,Button3,Button4,Button5,Button6;
Pulsaciones* but1,but2,but3,but4,but5,but6;
Pulsaciones* But[6];
void menuCloseCallback(cocos2d::Ref* pSender);
void update(float dt);
void TimerMethod(float dt);
cocos2d::Label *label;
cocos2d::Label *label1;
float time;
float minute;
float laps[3];
int lapIndex;
void IncrementTimer();
void DecrementTimer();
void combination();
void updatecode1();
void combinations(const vector<Pulsaciones*> &elems, unsigned long req_len);
void combinations_recursive(const vector<Pulsaciones*> &elems, unsigned long req_len,vector<unsigned long> &pos, unsigned long depth,unsigned long margin);
const unsigned long comb_len = 5;
const unsigned long num_elements = 6;
private:cocos2d::Size visibleSize;
cocos2d::Vec2 origin;
bool isDelete;
bool isCombine;
// Vec2 positions[5];
vector<Vec2>positions;
std::vector<Pulsaciones*>Pulsacionescode;
std::vector<Pulsaciones*>elements;
vector<Vec2>combine;
list<Pulsaciones*>BigList;
};#endif __HELLOWORLD_SCENE_H__
in cpp
#include "HelloWorldScene.h"
#include "Pulsaciones.h"
#include <iostream>
#include <string>
#include <sstream>
#include "extensions/cocos-ext.h"
#include "ui/CocosGui.h"
#include "cocos2d.h"
#include <vector>
#include <algorithm>
#include <list>
#include <cassert>
#include <array>
#define COCOS2D_DEBUG 1
USING_NS_CC;
using namespace cocos2d;
using namespace ui;
using std::copy;
using std::cout;
using std::endl;
using std::vector;
using namespace std;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();// add layer as a child to scene
scene->addChild(layer);// return the scene
return scene;
}// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();auto spritecache =SpriteFrameCache::getInstance();
spritecache->addSpriteFramesWithFile("sprites.plist");
//display
auto backgrounddisplay=Sprite::createWithSpriteFrameName("backgrounddisplay.png");
backgrounddisplay->setPosition(Vec2(visibleSize.width/2 +origin.x, visibleSize.height/2+origin.y));
this->addChild(backgrounddisplay,-5);
auto frontdisplay=Sprite::createWithSpriteFrameName("frontdisplay.png");
frontdisplay->setPosition(Vec2(visibleSize.width/2 +origin.x, visibleSize.height/2+origin.y));
this->addChild(frontdisplay,1);
//timer
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
label = Label::createWithTTF("00", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));// add the label as a child to this layer
this->addChild(label, 1);
label1 = Label::createWithTTF("05", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label1->setPosition(Vec2(origin.x + visibleSize.width/2.5,
origin.y + visibleSize.height - label->getContentSize().height));// add the label as a child to this layer
this->addChild(label1, 1);time=60;
minute=05;
lapIndex=0;
this->schedule(schedule_selector( HelloWorld::TimerMethod ),1);
//positionsVec2 first = Vec2(frontdisplay->getPositionX()- frontdisplay->getContentSize().width/-2.5,
frontdisplay->getPositionY()+frontdisplay->getContentSize().height/-26);Vec2 uno = Vec2(frontdisplay->getPositionX()- frontdisplay->getContentSize().width/-2.5,
frontdisplay->getPositionY()+frontdisplay->getContentSize().height/-26*1.25);
Vec2 second = Vec2(frontdisplay->getPositionX()- frontdisplay->getContentSize().width/-4.1,
frontdisplay->getPositionY()+frontdisplay->getContentSize().height/-26);Vec2 dos = Vec2(frontdisplay->getPositionX()- frontdisplay->getContentSize().width/-4.1,
frontdisplay->getPositionY()+frontdisplay->getContentSize().height/-26*1.25);
Vec2 third = Vec2(frontdisplay->getPositionX()- frontdisplay->getContentSize().width/-11.8,
frontdisplay->getPositionY()+frontdisplay->getContentSize().height/-26);Vec2 tres = Vec2(frontdisplay->getPositionX()- frontdisplay->getContentSize().width/-11.8,
frontdisplay->getPositionY()+frontdisplay->getContentSize().height/-26*1.25);
Vec2 fourth = Vec2(frontdisplay->getPositionX()-frontdisplay->getContentSize().width/11.7,
frontdisplay->getPositionY()+ frontdisplay->getContentSize().height/-26);
Vec2 cuatro = Vec2(frontdisplay->getPositionX()-frontdisplay->getContentSize().width/11.7,
frontdisplay->getPositionY()+ frontdisplay->getContentSize().height/-26*1.25);
Vec2 fifth = Vec2(frontdisplay->getPositionX()-frontdisplay->getContentSize().width/4.1,
frontdisplay->getPositionY()+ frontdisplay->getContentSize().height/-26);Vec2 cinco = Vec2(frontdisplay->getPositionX()-frontdisplay->getContentSize().width/4.1,
frontdisplay->getPositionY()+ frontdisplay->getContentSize().height/-26*1.25);
/*Vec2 sixth = Vec2(frontdisplay->getPositionX()-frontdisplay->getContentSize().width/2.5,
frontdisplay->getPositionY()+ frontdisplay->getContentSize().height/-26);*/setCode(first,second,third,fourth,fifth/*,sixth*/);
setCode1(uno,dos,tres,cuatro,cinco);//sprite and position
//buttons
ui::Button *button1= ui::Button::create("GameScreen/button1.png","GameScreen/button_press1.png");
button1->setPosition(Vec2(visibleSize.width/2.85 ,visibleSize.height/3.2 ));
this->addChild(button1);
button1->addTouchEventListener( CC_CALLBACK_2( HelloWorld::Button1touchEvent, this ) );ui::Button *button2= ui::Button::create("GameScreen/button2.png","GameScreen/button_press2.png");
button2->setPosition(Vec2(visibleSize.width/2.85*1.25 ,visibleSize.height/3.2 ));
this->addChild(button2);
button2->addTouchEventListener( CC_CALLBACK_2( HelloWorld::Button2touchEvent, this ) );ui::Button *button3= ui::Button::create("GameScreen/button3.png","GameScreen/button_press3.png");
button3->setPosition(Vec2(visibleSize.width/2.85*1.50 ,visibleSize.height/3.2 ));
this->addChild(button3);
button3->addTouchEventListener( CC_CALLBACK_2( HelloWorld::Button3touchEvent, this ) );ui::Button *button4= ui::Button::create("GameScreen/button4.png","GameScreen/button_press4.png");
button4->setPosition(Vec2(visibleSize.width/2.85*1.75 ,visibleSize.height/3.2 ));
this->addChild(button4);
button4->addTouchEventListener( CC_CALLBACK_2( HelloWorld::Button4touchEvent, this ) );ui::Button *button5= ui::Button::create("GameScreen/button5.png","GameScreen/button_press5.png");
button5->setPosition(Vec2(visibleSize.width/2.85*2 ,visibleSize.height/3.2 ));
this->addChild(button5);
button5->addTouchEventListener( CC_CALLBACK_2( HelloWorld::Button5touchEvent, this ) );ui::Button *button6= ui::Button::create("GameScreen/button6.png","GameScreen/button_press6.png");
button6->setPosition(Vec2(visibleSize.width/2.85*2.25 ,visibleSize.height/3.2 ));
this->addChild(button6);
button6->addTouchEventListener( CC_CALLBACK_2( HelloWorld::Button6touchEvent, this ) );ui::Button *ButtonDelete= ui::Button::create("GameScreen/delete_normal.png","GameScreen/delete_pressbutton.png");
ButtonDelete->setPosition(Vec2(visibleSize.width/2.85 ,visibleSize.height/3.2*1.25 ));
this->addChild(ButtonDelete);
ButtonDelete->addTouchEventListener( CC_CALLBACK_2( HelloWorld::ButtonDeletetouchEvent, this ) );
ui::Button *ButtonEnter= ui::Button::create("GameScreen/enter_normal.png","GameScreen/enter_pressbuttom.png");
ButtonEnter->setPosition(Vec2(visibleSize.width/2.85*1.75 ,visibleSize.height/3.2*1.25 ));
this->addChild(ButtonEnter);
ButtonEnter->addTouchEventListener( CC_CALLBACK_2( HelloWorld::ButtonEntertouchEvent, this ) );
return true;
}//counter
int HelloWorld::i_count= 1;
int HelloWorld::u_count= 0;int HelloWorld::contador(){
if (false==isDelete){
u_count= i_count++;return u_count;
}
else if (u_count=0){}else if (u_count>0 && true==isDelete)
{return--u_count;
}
}//buttons
void HelloWorld::Button1touchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
if (false==isDelete){
Pulsaciones *Button1=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(1)+".png",getCode(contador()));
//Button1->setTag(1);
this->addChild(Button1);Pulsacionescode.push_back(Button1);
this->scheduleUpdate();
}
else{
this->updatecode();
}
break;
case Widget::TouchEventType::ENDED:
// code to handle when the button click has ended (e.g. finger is lifted off the screen)
break;
}
}void HelloWorld::Button2touchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
Pulsaciones *Button2=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(2)+".png",getCode(contador()));
// Button2->setTag(2);
this->addChild(Button2);
Pulsacionescode.push_back(Button2);
this->scheduleUpdate(); // code to handle when the button is first clicked
break;
}
}
void HelloWorld::Button3touchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
Pulsaciones *Button3=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(3)+".png",getCode(contador()));
// Button3->setTag(3);
this->addChild(Button3);
Pulsacionescode.push_back(Button3);
this->scheduleUpdate();
break;
}
}
void HelloWorld::Button4touchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
Pulsaciones *Button4=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(4)+".png",getCode(contador()));
// Button4->setTag(4);
this->addChild(Button4);
Pulsacionescode.push_back(Button4);
this->scheduleUpdate();
break;
}
}
void HelloWorld::Button5touchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
Pulsaciones *Button5=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(5)+".png",getCode(contador()));
// Button5->setTag(5);
this->addChild(Button5);
Pulsacionescode.push_back(Button5);
this->scheduleUpdate();// code to handle when the button is first clicked
break;
}
}
void HelloWorld::Button6touchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{case Widget::TouchEventType::BEGAN:
Pulsaciones *Button6=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(6)+".png",getCode(contador()));
//Button6->setTag(6);
this->addChild(Button6);
Pulsacionescode.push_back(Button6);
this->scheduleUpdate();
// code to handle when the button is first clicked
break;
}
}
//vector positions setter
////execution
void HelloWorld::ButtonDeletetouchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
isDelete=true;
updatecode1();// code to handle when the button is first clicked
break;
case Widget::TouchEventType::ENDED:
isDelete=false;
HelloWorld::i_count= 1;
break;
}
}void HelloWorld::ButtonEntertouchEvent( Ref *sender, Widget::TouchEventType type )
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
combination();
updatecode1();
// code to handle when the button is first clicked
break;
}
}void HelloWorld::setCode(Vec2 uno ,Vec2 dos ,Vec2 tres ,Vec2 cuatro ,Vec2 cinco/*,Vec2 seis*/)
{Vec2 *Uno;
Uno=&uno;
Vec2 *Dos;
Dos=&dos;
Vec2 *Tres;
Tres=&tres;
Vec2 *Cuatro;
Cuatro=&cuatro;
Vec2 *Cinco;
Cinco=&cinco;
/*Vec2 *Seis;
Seis=&seis;*/positions.push_back(*Uno);
positions.push_back(*Dos);
positions.push_back(*Tres);
positions.push_back(*Cuatro);
positions.push_back(*Cinco);
//positions[5] = *Seis;*/
}
//getter
Vec2 HelloWorld::getCode(int counter){
int count;
count=counter-1;
if (count<0){
}
else if (count>=0 && count<=5){
return positions[count];
}else{
}
}void HelloWorld::updatecode()
{for (int i = 0; i <Pulsacionescode.size(); i++)
{
if (true==isDelete)
{
Pulsaciones* Button1 = (Pulsaciones*)Pulsacionescode[i];
Button1->update();
this->removeChild(Button1);
Pulsacionescode.erase(Pulsacionescode.begin() + i);
}
}}
//
void HelloWorld::update(float dt)
{
updatecode();
//updatecode1();
CCLOG("UPDATE");
}void HelloWorld::menuCloseCallback(Ref* pSender)
{
if (lapIndex < 3)
{laps[lapIndex]=time;
lapIndex++;
log("Lap %: %.f",lapIndex,time);
time=0;
}
}
void HelloWorld::TimerMethod(float dt)
{
time -=dt;
if (time<10){
__String *timeToDisplay =__String::createWithFormat(":0%.f",time);
label->setString(timeToDisplay->getCString());
}
else{
__String *timeToDisplay =__String::createWithFormat(":%.f",time);
label->setString(timeToDisplay->getCString());
}if (time==59)
{
--minute ;
if (minute<10){
__String *timeToDisplay1 =__String::createWithFormat("0%.f",minute);
label1->setString(timeToDisplay1->getCString());
}else
{__String *timeToDisplay1 =__String::createWithFormat("%.f",minute);
label1->setString(timeToDisplay1->getCString());
}}
else if ( minute==0 && time==0)
{minute=01;
this->unschedule( schedule_selector( HelloWorld::TimerMethod ) );}
else if (time==0.00)
{time=60;
}
}
void HelloWorld::IncrementTimer()
{
time+10;}
void HelloWorld::DecrementTimer()
{
time-5;}
//algorithm
//const unsigned long num_elements = 6;
//const unsigned long comb_len = 5;
void HelloWorld::combination()
{
const unsigned long comb_len = 5;
const unsigned long num_elements = 6;
Pulsaciones *but1=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(1)+".png",getCode1(contador1()));
//elements.push_back(but1);
//but1->setTag(1);
Pulsaciones *but2=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(2)+".png",getCode1(contador1()));
//but2->setTag(2);
//elements.push_back(but2);
Pulsaciones *but3=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(3)+".png",getCode1(contador1()));
//but3->setTag(3);
//elements.push_back(but3);
Pulsaciones *but4=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(4)+".png",getCode1(contador1()));
//but4->setTag(4);
//elements.push_back(but4);
Pulsaciones *but5=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(5)+".png",getCode1(contador1()));
//but5->setTag(5);
//elements.push_back(but5);Pulsaciones *but6=Pulsaciones::createWithSpriteFrameName("signal" + std::to_string(6)+".png",getCode1(contador1()));
//but6->setTag(6);
//elements.push_back(but6);
But[0]=but1;
But[1]=but2;
But[2]=but3;
But[3]=but4;
But[4]=but5;
But[5]=but6;
//elements(num_elements);
// elements(num_elements);std::vector<Pulsaciones*>elements(num_elements);
Pulsaciones* elements_str[num_elements + 1] = {but1,but2,but3,but4,but5,but6};
copy(elements_str, elements_str + num_elements, elements.begin());
combinations(elements, comb_len);
}
void HelloWorld::combinations(const vector<Pulsaciones*> &elems, unsigned long req_len)
{
assert(req_len > 0 && req_len <= elems.size());
vector<unsigned long> positions(req_len, 0);
combinations_recursive(elems, req_len, positions, 0, 0);
}
void HelloWorld::combinations_recursive(const vector<Pulsaciones*> &elems, unsigned long req_len,vector<unsigned long> &pos, unsigned long depth,unsigned long margin)
{
// Have we selected the number of required elements?
if (depth >= req_len) {
for (unsigned long ii = 0; ii < pos.size(); ++ii){
this->addChild(elems[pos[ii]],5);
}
return;
}// Are there enough remaining elements to be selected?
// This test isn't required for the function to be correct, but
// it can save a good amount of futile function calls.
if ((elems.size() - margin) < (req_len - depth))
return;// Try to select new elements to the right of the last selected one.
for (unsigned long ii = margin; ii < elems.size(); ++ii) {
pos[depth] = ii;
combinations_recursive(elems, req_len, pos, depth + 1, ii + 1);
}
return;
}
void HelloWorld::setCode1(Vec2 uno ,Vec2 dos ,Vec2 tres ,Vec2 cuatro ,Vec2 cinco/*,Vec2 seis*/)
{Vec2 *Uno;
Uno=&uno;
Vec2 *Dos;
Dos=&dos;
Vec2 *Tres;
Tres=&tres;
Vec2 *Cuatro;
Cuatro=&cuatro;
Vec2 *Cinco;
Cinco=&cinco;
/*Vec2 *Seis;
Seis=&seis;*/combine.push_back(*Uno);
combine.push_back(*Dos);
combine.push_back(*Tres);
combine.push_back(*Cuatro);
combine.push_back(*Cinco);
//positions[5] = *Seis;*/
}
//getter
int HelloWorld::j_count= 1;
int HelloWorld::k_count= 0;
int HelloWorld::contador1(){
if (false==isDelete){
k_count= j_count++;return k_count;
}
else if (k_count=0){}else if (k_count>0 && true==isDelete)
{return--k_count;
}
}
int HelloWorld::r_count=0;
Vec2 HelloWorld::getCode1(int counter){
int count;count=counter-1;
if (count<0){
}
else if (count>=0 && count<=5){
r_count++;
return combine[count];
}else if (r_count=4)
{
j_count= 1;
k_count= 0;
r_count=0;
}
else{
}
}
void HelloWorld::updatecode1()
{
for (int i = 0; i <elements.size(); i++)
{
-
hamletLikes 0
the problem is in helloworld.h
vector<Pulsaciones*>elements; is private.
than i need initialized in helloworld.cpp, however when i try to initialized inside of the funtion void conbination(), elements=std::vector<Pulsaciones*>(num_elements), the funtion only print a partial sprite of the hole vector (for example if i make inside of void combination a new vector std::vector<Pulsaciones*>elements(num_elements); then inside rum well ,however i can not used to the funtion void update1(); for erase the elements inside of vector elements.
i was tring make intantiation with elements=std::vector<Pulsaciones*>(num_elements) but not found well.
-
Sonar Systems adminLikes 0
Have you tried a simple vector with integers to see if that works?
-
hamletLikes 0
Guten Morgen
i was trying this before but when i increase num_elements to 24 and conb_elem to 6 thas go up the max_length of integrer and crash the game(this is the total of button of the game ). i was declared a simple vector too inside of void combination; the vector as local vector go well but i can not call for other function like void update1() for used to erase the hole vector. like i can make with te vector std::vectorPulsacionescode;
-
Sonar Systems adminLikes 0
Have you trued an array instead?
Login to reply