I am drawing the Custom slider control. But my tic marks are getting erased on moving the thumb. I draw the ticks in ITEMPREPAINT as below:
if( lpcd->dwDrawStage == CDDS_ITEMPREPAINT )
{
if(lpcd->dwItemSpec == TBCD_TICS)
{
CDC *pDC = CDC::FromHandle(lpcd->hdc);
INT nMin=0,nMax=0,range;
GetRange(nMin,nMax);
range = nMax - nMin;
INT num_tics = 4;
INT frequency = range/num_tics;
CRect channelRect, thumbRect;
GetChannelRect(&channelRect);
GetThumbRect(&thumbRect);
INT width,pos=channelRect.left;
/*pDC->MoveTo(pos,channelRect.bottom);
pDC->LineTo(pos,channelRect.bottom+10);*/
for (INT tic=0; tic<num_tics; tic++)
{
width = channelRect.right - channelRect.left;
pos += width/num_tics;
pDC->MoveTo(pos,channelRect.bottom);
pDC->LineTo(pos,channelRect.bottom+10);
}
*pResult = CDRF_SKIPDEFAULT;
return;
}
}
Related
There are 5 polygonal areas in the window. If the user's mouse moves over one of the polygon, it shows the Message Box.
The code is like below:
void OnMouseMove(UINT nFlags, CPoint point){
CPoint pt[5][6];
//The points of polygon0
pt[0][0].x = 18;
pt[0][0].y = 639;
pt[0][1].x = -4;
pt[0][1].y = 688;
pt[0][2].x = 17;
pt[0][2].y = 707;
pt[0][3].x = 42;
pt[0][3].y = 683;
pt[0][4].x = 47;
pt[0][4].y = 637;
pt[0][5].x = 80;
pt[0][5].y = 638;
//The points of polygon1
pt[1][0].x = 47;
pt[1][0].y = 637;
pt[1][1].x = 42;
pt[1][1].y = 683;
pt[1][2].x = 17;
pt[1][2].y = 707;
pt[1][3].x = 56;
pt[1][3].y = 714;
pt[1][4].x = 89;
pt[1][4].y = 674;
pt[1][5].x = 80;
pt[1][5].y = 638;
//The points of polygon2
pt[2][0].x = 80;
pt[2][0].y = 638;
pt[2][1].x = 89;
pt[2][1].y = 674;
pt[2][2].x = 56;
pt[2][2].y = 714;
pt[2][3].x = 101;
pt[2][3].y = 713;
pt[2][4].x = 126;
pt[2][4].y = 675;
pt[2][5].x = 111;
pt[2][5].y = 637;
//The points of polygon3
pt[3][0].x = 111;
pt[3][0].y = 637;
pt[3][1].x = 126;
pt[3][1].y = 675;
pt[3][2].x = 101;
pt[3][2].y = 713;
pt[3][3].x = 143;
pt[3][3].y = 713;
pt[3][4].x = 166;
pt[3][4].y = 676;
pt[3][5].x = 166;
pt[3][5].y = 638;
//The points of polygon4
pt[4][0].x = 166;
pt[4][0].y = 638;
pt[4][1].x = 166;
pt[4][1].y = 676;
pt[4][2].x = 143;
pt[4][2].y = 713;
pt[4][3].x = 173;
pt[4][3].y = 713;
pt[4][4].x = 206;
pt[4][4].y = 679;
pt[4][5].x = 188;
pt[4][5].y = 638;
HRGN hrgn[5];
for(int i = 0; i < 5; i++)
hrgn[i] = CreatePolygonRgn(pt[i], 6, ALTERNATE);
if(PtInRegion(hrgn[0], point.x, point.y)){
//Your mouse is moving over polygon0
MessageBox(L"Your mouse is moving over polygon0");
}else if(PtInRegion(hrgn[1], point.x, point.y)){
//Your mouse is moving over polygon1
MessageBox(L"Your mouse is moving over polygon1");
}else if(PtInRegion(hrgn[2], point.x, point.y)){
//Your mouse is moving over polygon2
MessageBox(L"Your mouse is moving over polygon2");
}else if(PtInRegion(hrgn[3], point.x, point.y)){
//Your mouse is moving over polygon3
MessageBox(L"Your mouse is moving over polygon3");
}else if(PtInRegion(hrgn[4], point.x, point.y)){
//Your mouse is moving over polygon4
MessageBox(L"Your mouse is moving over polygon4");
}else{
//Other events...
}
DeleteObject(pt);
DeleteObject(hrgn);
}
It seems like there's no problem about the code. But if I try to move the mouse cursor in and out the polygons for many many times, it may cause an Access Violation problem (AV problem). So what causes the AV problem? Is there any better way to deal with it?
I'm putting this here because the algorithm for doing this is more difficult to find than it should be. Hopefully Google will cache this.
The problem is: you have a bitmap and a window. You want to draw the bitmap inside a window, filling the window, keeping the aspect ratio, as the window resizes.
You may also want to be able to fit it the other way, so that you can draw the image "over" the window, and all the area in the window will be filled. This will clip out some of the image. I present in the answer a simple algorithm for doing so.
Here's an implementation that uses integer math only.
The algorithm first stretches both dimensions, preserving aspect ratio. The new size is calculated, assuming that the respective other dimension occupies the entire space. Of these new dimensions, the one that overshoots the available area is set to the maximum possible value, while the other is scaled back, preserving aspect ratio. (For pan and scan (bScale is set to true) mode, the dimension that doesn't overshoot the available space is set to occupy the entire range.)
(Note: If sizePicture is an empty rectangle, this function returns a rectangle that stretches one pixel to the left and one pixel up, either from the origin, or the center.)
RECT size_rect( RECT& rcScreen,
RECT& sizePicture,
bool bCenter/*,
bool bScale*/ ) {
int clientWidth = rcScreen.right - rcScreen.left;
int clientHeight = rcScreen.bottom - rcScreen.top;
int picWidth = sizePicture.right - sizePicture.left;
int picHeight = sizePicture.bottom - sizePicture.top;
// Calculate new content size
int contentWidth = ::MulDiv( clientHeight, picWidth, picHeight );
int contentHeight = ::MulDiv( clientWidth, picHeight, picWidth );
// Adjust dimensions to fit inside client area
if ( contentWidth > clientWidth ) {
// To use the bScale parameter that allows the image to fill the entire
// client area, use the following if-clause instead.
//if ( ( bScale && ( contentWidth < clientWidth ) )
// || ( !bScale && ( contentWidth > clientWidth ) ) ) {
contentWidth = clientWidth;
contentHeight = ::MulDiv( contentWidth, picHeight, picWidth );
} else {
contentHeight = clientHeight;
contentWidth = ::MulDiv( contentHeight, picWidth, picHeight );
}
RECT rect = { 0 };
::SetRect( &rect, 0, 0, contentWidth, contentHeight );
if ( bCenter ) {
// Calculate offsets to center content
int offsetX = ( clientWidth - contentWidth ) / 2;
int offsetY = ( clientHeight - contentHeight ) / 2;
::OffsetRect( &rect, offsetX, offsetY );
}
return rect;
}
Make two RECT. One is the window you wish to fit to (passed into rcScreen), and the other holds the dimensions of the picture:
(pseudo-code)
RECT window;
GetClientRect(hwnd,&window)
RECT bitmap_rect;
BITMAP bitmap;
bitmap_rect.left = bitmap_rect.top = 0;
bitmap_rect.right = bitmap.bmWidth;
bitmap_rect.bottom = bitmap.bmHeight;
RECT draw_rect = size_rect(window,bitmap_rect,true,true);
Then StretchBlt it:
StretchBlt(toDC, draw_rect.left, draw_rect.top, draw_rect.right, draw_rect.bottom, fromDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
This is the function: (note there is no case for bCenter = false and Scale = true). **bCenter is flag for "center picture in window." Scale is flag for "pan and scan mode" instead of "letterbox," useful if you are using an image as a window background that you want resized but don't want to have letterboxes. **
RECT size_rect(RECT& rcScreen,
RECT& sizePicture,
bool bCenter,
bool Scale)
{
RECT rect = rcScreen;
double dWidth = rcScreen.right - rcScreen.left;
double dHeight = rcScreen.bottom - rcScreen.top;
double dAspectRatio = dWidth / dHeight;
double dPictureWidth = sizePicture.right - sizePicture.left;
double dPictureHeight = sizePicture.bottom - sizePicture.top;
double dPictureAspectRatio = dPictureWidth / dPictureHeight;
double nNewHeight = dHeight;
double nNewWidth = dWidth;
double nHeightCenteringFactor = 0;
double nWidthCenteringFactor = 0;
double xstart = rcScreen.left;
double ystart = rcScreen.top;
if (dPictureAspectRatio > dAspectRatio)
{
if (bCenter && Scale) {
nNewWidth = dPictureWidth*(1 / (dPictureHeight / dHeight));
xstart = rcScreen.left - ((nNewWidth / 2) - (dWidth / 2));
}
else {
nNewHeight = (int)(dWidth / dPictureWidth*dPictureHeight);
if (bCenter)
ystart = ((dHeight - nNewHeight) / 2) + rcScreen.top;
}
}
else if (dPictureAspectRatio < dAspectRatio)
{
if (bCenter && Scale) {
nNewHeight = dPictureHeight*(1 / (dPictureWidth / dWidth));
ystart = rcScreen.top - ((nNewHeight / 2) - (dHeight / 2));
}
else{
nNewWidth = (dHeight / dPictureHeight*dPictureWidth);
if (bCenter)
xstart = ((dWidth - nNewWidth) / 2) + rcScreen.left;
}
}
SetRect(&rect, xstart, ystart, nNewWidth, nNewHeight);
return rect;
}
I'm going to get straight to the point:
I have a sprite sheet that has 8 movement sprites (http://i.imgur.com/hLpo2Qn.png cant post images :\ ), so it creates the illusion that im walking)
My problem is that when i press and hold a key i want the sprite to kinda have an animated walk. For example when i press the up arrow the first two sprites play in a loop until i let go of the key. All I've been able to do so far is to get only one sprite showing for each direction, so it kinda gives an unrealistic effect.
Code:
#include <SDL/SDL.h>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen, *image;
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
bool running = true;
const int FPS = 30;
Uint32 start;
bool direction[4] = {0, 0, 0, 0};
SDL_Rect pos;
pos.x = 0;
pos.y = 0;
SDL_Rect sprite;
sprite.x = 160;
sprite.y = 0;
sprite.w = 32;
sprite.h = 32;;
Uint32 color = SDL_MapRGB(screen -> format, 0xff, 0xff, 0xff);
image = SDL_DisplayFormat(SDL_LoadBMP("sprite.bmp"));
SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen -> format, 255, 0, 255));
while (running == true)
{
start = SDL_GetTicks();
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
direction[0] = 1;
sprite.x = 0;
break;
case SDLK_LEFT:
direction[1] = 1;
sprite.x = 224;
break;
case SDLK_DOWN:
direction[2] = 1;
sprite.x = 160;
break;
case SDLK_RIGHT:
direction[3] = 1;
sprite.x = 96;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
direction[0] = 0;
sprite.x = 32;
break;
case SDLK_LEFT:
direction[1] = 0;
break;
case SDLK_DOWN:
direction[2] = 0;
break;
case SDLK_RIGHT:
direction[3] = 0;
break;
}
break;
}
}
if(direction[0])
pos.y-- ;
if(direction[1])
pos.x--;
if(direction[2])
pos.y++;
if(direction[3])
pos.x++;
SDL_FillRect(screen, &screen -> clip_rect, color);
SDL_BlitSurface(image, &sprite, screen, &pos);
SDL_Flip(screen);
if(1000 / FPS > SDL_GetTicks() - start)
SDL_Delay(1000 / FPS - (SDL_GetTicks() - start));
}
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
Disclaimer: This is not the only or best way to achieve what you wanting. It is simply one possible solution
You need to implement a basic timer/counter to switch between the two appropriate images.
For this example I would store your sprite offsets in a 2D array int sprite_x[4][2]; - this would represent the four directions and the two image offsets for each. Then on a key press instead of using a bool array just have a single integer index int sprite_index; for indexing the sprite offsets. All you would then need to do is have another integer value called something like int current_key; which you would switch between 0 and 1.
// Used for indexing the 2D array
enum
{
WALK_LEFT,
WALK_RIGHT,
WALK_UP,
WALK_DOWN
WALK_MAX
};
// How many sprites per animation
const int NUM_KEYFRAMES = 2;
// 2D array to hold the x value offsets for sprites
int sprite_x[WALK_MAX][NUM_KEYFRAMES];
int sprite_index = WALK_LEFT; // Current animation
int current_key = 0; // Current keyframe for the animation
// Set up all the x value offsets in the array
sprite_x[WALK_LEFT][0] = 0;
sprite_x[WALK_LEFT][1] = 32;
sprite_x[WALK_RIGHT][0] = 64;
sprite_x[WALK_RIGHT][1] = 96;
.
.
.
You then simply update the sprite x value accordingly sprite.x = sprite_x[sprite_index][current_key];
I want to resize my CListCtrl as Report for displaying only n rows with text or empty. And I need the height of a row (they all are of the same size and constant).
CRect rect;
myList.GetWindowRect(&rect);
ScreenToClient(&rect);
myList.MoveWindow(rect.left, rect.top, rect.Width(), 16*8-5 /* TODO */);
CListCtrl with Variable Row Height
/*
1a. Setup a typical CListCtrl with owner draw
1b. Fill the CListCtrl with the text you want, as you would normally
2. Setup a CListBox with OwnerDrawVariable and NO border
3. Make the ListBox a child of the ListCtrl
4. Use OnSize to position and OnDrawItem to display the text
Note the OnDrawItem is using the text and some parameters from the CListCtrl
so be mindful of m_lbTest vs. m_lcTest.
*/
void CTestDlg::FillListCtrl()
{
// fill CListCtrl with some text
int x,y;
int ColCount;
CString csStr;
m_lbTest.SetParent(&m_lcTest);
ColCount=m_lcTest.GetHeaderCtrl()->GetItemCount();
for (y=0; y<10; y++) {
m_lcTest.InsertItem(y,"");
for (x=0; x<ColCount; x++) {
csStr.Format("y=%d x=%d",y,x);
m_lcTest.SetItemText(y,x,csStr);
}
m_lbTest.AddString("");
m_lbTest.SetItemHeight(y,20 + y*10);
}
}
void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
int x,y;
CRect rb;
if (m_lcTest.m_hWnd != NULL) {
y=0;
x=0;
m_lcTest.SetWindowPos(&wndTop,x,y,cx,cy-y,SWP_NOZORDER);
m_lcTest.GetHeaderCtrl()->GetClientRect(&rb);
x=0;
y+=rb.Height();
m_lbTest.SetWindowPos(&wndTop,x,rb.Height(),cx-x-4,cy-y-4,SWP_NOZORDER);
}
}
void CTestDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpD)
{
CString csStr;
CRect rc;
CRect rb;
UINT fmt;
LVCOLUMN lvc;
int x,y;
CDC dc;
int ColCount;
CPen cPen;
CopyRect(&rc,&lpD->rcItem);
dc.Attach(lpD->hDC);
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));
cPen.CreatecPen.CreatePen(PS_SOLID,1,RGB(192,192,192));
dc.SelectObject(cPen);
dc.SetTextColor(RGB(0,0,0));
if (m_lbTest.m_hWnd!=NULL && nIDCtl==IDC_LB_TEST) {
y=lpD->itemID;
if (y >= 0) {
rc.left+=5;
ColCount=m_lcTest.GetHeaderCtrl()->GetItemCount();
for (x=0; x<ColCount; x++){
rc.right=rc.left + m_lcTest.GetColumnWidth(x) - 10;
rb.top =rc.top+1;
rb.bottom=rc.bottom;
rb.left =rc.left - 5;
rb.right =rc.right + 4;
if ((lpD->itemState&ODS_SELECTED) != 0) dc.FillSolidRect(&rb,RGB(255,255,192)); // light yellow
else dc.FillSolidRect(&rb,RGB(255,255,255));
rb.top--;
dc.MoveTo(rb.left-1,rb.bottom);
dc.LineTo(rb.right, rb.bottom);
dc.LineTo(rb.right, rb.top);
dc.LineTo(rb.left-1,rb.top);
lvc.mask=LVCF_FMT;
m_lcTest.GetColumn(x,&lvc);
if ((lvc.fmt&LVCFMT_RIGHT) != 0) fmt=DT_RIGHT;
else if ((lvc.fmt&LVCFMT_CENTER) != 0) fmt=DT_CENTER;
else fmt=DT_LEFT;
fmt|=DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX;
csStr=m_lcTest.GetItemText(y,x);
dc.DrawText(csStr,&rc,fmt);
rc.left=rc.right + 10;
}
}
}
cPen.DeleteObject();
dc.Detach();
}
I'm a beginner that just followed cocos2d-x's native tutorials and I am faced with a huge wall!
This is my error:
>c:\cocos2d-2.0-x-2.0.3\cocos2dsimplegame\classes\helloworldscene.cpp(86): error C2440: 'type cast' : cannot convert from 'void (__thiscall HelloWorld::* )(cocos2d::CCTime)' to 'cocos2d::SEL_SCHEDULE'
>Pointers to members have different representations; cannot cast between them
My cpp file:
#include "HelloWorldScene.h"
using namespace cocos2d;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
_targets = CCArray::create();
_projectiles = CCArray::create();
do {
////////////////////
// super init first
////////////////////
if ( !CCLayerColor::initWithColor( ccc4(255, 255, 255, 255) ) )
{
return false;
}
////////////////////
// add your codes below..
////////////////////
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemWithNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
//Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
//Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
//////////////////////
// 2. add your codes below...
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *player = CCSprite::spriteWithFile("Player.png", CCRectMake(0, 0, 27, 40));
player->setPosition(ccp(player->getContentSize().width/2, winSize.height/2));
this->addChild(player);
bRet = true;
} while (0);
// Call game logic about every second
this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0);
// You can shoot the bullet
this->setTouchEnabled(true);
this->schedule( schedule_selector(HelloWorld::update) );
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
void HelloWorld::addTarget()
{
CCSprite *target = CCSprite::spriteWithFile("Target.png", CCRectMake(0, 0, 27, 40));
// Determine where to spawn the target along the Y axis
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int minY = target->getContentSize().height/2;
int maxY = winSize.height - target->getContentSize().height/2;
int rangeY = maxY - minY;
// srand( TimGetTicks() );
int actualY = ( rand() % rangeY ) + minY;
// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated
target->setPosition( ccp(winSize.width + (target->getContentSize().width/2), actualY) );
this->addChild(target);
// Determine speed of the target
int minDuration = (int)2.0;
int maxDuration = (int)4.0;
int rangeDuration = maxDuration - minDuration;
// srand( TimGetTicks() );
int actualDuration = ( rand() % rangeDuration ) + minDuration;
// Create the actions
CCFiniteTimeAction* actionMove = CCMoveTo::actionWithDuration( (float)actualDuration, ccp(0 - target->getContentSize().width/2, actualY) );
CCFiniteTimeAction* actionMoveDone = CCCallFuncN::actionWithTarget( this, callfuncN_selector(HelloWorld::spriteMoveFinished) );
target->runAction( CCSequence::actions( actionMove, actionMoveDone, NULL) );
// Add to targets array
target->setTag(1);
_targets->addObject(target);
}
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite *sprite = (CCSprite *)sender;
this->removeChild(sprite, true);
if (sprite->getTag() == 1) // target
{
_targets->removeObject(sprite);
}
else if (sprite->getTag() == 2) // projectile
{
_projectiles->removeObject(sprite);
}
}
void HelloWorld::gameLogic(float dt)
{
this->addTarget();
}
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
// Choose one of the touches to work with
CCTouch* touch = (CCTouch*)( touches->anyObject() );
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
// Set up initial location of projectile
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *projectile = CCSprite::spriteWithFile("Projectile.png", CCRectMake(0, 0, 20, 20));
projectile->setPosition( ccp(20, winSize.height/2) );
// Determine offset of location to projectile
int offX = location.x - projectile->getPosition().x;
int offY = location.y - projectile->getPosition().y;
// Bail out if we are shooting down or backwards
if (offX <= 0) return;
// OK to add now - we've double checked position
this->addChild(projectile);
// Determine where we wish to shoot the projectile to
int realX = winSize.width + (projectile->getContentSize().width/2);
float ratio = (float)offY / (float)offX;
int realY = (realX * ratio) + projectile->getPosition().y;
CCPoint realDest = ccp(realX, realY);
// Determine the length of how far we're shooting
int offRealX = realX - projectile->getPosition().x;
int offRealY = realY - projectile->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY * offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
projectile->runAction( CCSequence::actions( CCMoveTo::actionWithDuration(realMoveDuration, realDest), CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) );
// Add to projectiles array
projectile->setTag(2);
_projectiles->addObject(projectile);
}
void HelloWorld::update(CCTime dt)
{
CCArray *projectilesToDelete = CCArray::create();
CCObject* arrayItem1;
CCARRAY_FOREACH(_projectiles, arrayItem1)
{
CCSprite* projectile = (CCSprite*)arrayItem1;
CCRect projectileRect = CCRectMake(
projectile->getPosition().x - (projectile->getContentSize().width/2),
projectile->getPosition().y - (projectile->getContentSize().height/2),
projectile->getContentSize().width,
projectile->getContentSize().height);
CCArray* targetsToDelete = CCArray::create();
CCObject* arrayItem2;
CCARRAY_FOREACH(_targets, arrayItem2)
{
CCSprite* target = (CCSprite*) arrayItem2;
CCRect targetRect = CCRectMake(
target->getPosition().x - (target->getContentSize().width/2),
target->getPosition().y - (target->getContentSize().height/2),
target->getContentSize().width,
target->getContentSize().height);
if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
{
targetsToDelete->addObject(target);
}
}
CCARRAY_FOREACH(targetsToDelete, arrayItem2)
{
CCSprite* target = (CCSprite*) arrayItem2;
_targets->removeObject(target);
this->removeChild(target, true);
}
if (targetsToDelete->count() > 0)
{
projectilesToDelete->addObject(projectile);
}
targetsToDelete->release();
}
CCARRAY_FOREACH(projectilesToDelete, arrayItem1)
{
CCSprite* projectile = (CCSprite*) arrayItem1;
_projectiles->removeObject(projectile);
this->removeChild(projectile, true);
}
projectilesToDelete->release();
}
Maybe this part is a problem:
bool HelloWorld::init()
{
....
this->schedule( schedule_selector(HelloWorld::update) );
....
}
But I don't understand why this part is a problem.
Please, help me!
change CCTime to float.
in old cocos2d-x version, they have ccTime instead of CCTime
but in 2.0 they remove it. since it is duplicated with float.