I have a problem about spawn of objects every 2 seconds

//I have a problem about spawn of objects every 2 seconds, in my code, it shows //the sprite by blinking every 2 seconds. can anyone help me with this? i want //it to be spawn the car2 every 2 seconds .



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

// Beginning Game Programming, 2nd Edition
// Chapter 8
// Tiled_Sprite program source code file

#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <d3d9.h>
#include "timer.h"

#define car2num 5

LPDIRECT3DTEXTURE9 maincar_image, car2_image;
SPRITE maincar;

SPRITE car2[car2num];
LPDIRECT3DSURFACE9 back, intro;
LPD3DXSPRITE sprite_handler;
LPD3DXSPRITE car2_handler;
 

#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")

HRESULT result, result2;
void introduction();
void startGame();
void car2start();


bool StartGame;
bool spawn;
//timing variable
long start = GetTickCount();
Timer timer;


//initializes the game
int Game_Init(HWND hwnd)
{
	//set random number seed
	srand(time(NULL));
	int first, second, third,r;
	
	
	
	first = 180;
	second = 355;
	third = 535;
	StartGame = false;
	spawn = false;

    //create sprite handler object
    result = D3DXCreateSprite(d3ddev, &sprite_handler);
    if (result != D3D_OK)
        return 0;

    //load texture with "pink/magenta" as the transparent color
    maincar_image = LoadTexture("maincar.png", D3DCOLOR_XRGB(255,0,255));
    if (maincar_image == NULL)
        return 0;
	

    //load the background image
    intro = LoadSurface("intro.png", NULL);
	if (intro == NULL)
		return 0;
	back = LoadSurface("flatbackground.bmp", NULL);
	if (back == NULL)
		return 0;

    //initialize the sprite's properties
	maincar.x = (SCREEN_WIDTH / 2 - 50 );
	maincar.y = SCREEN_HEIGHT-150;
    maincar.width = 100;  //location in the image file
    maincar.height = 150;  //location in the image file
    maincar.curframe = 0;  
    maincar.lastframe = 1;  //12 
	maincar.animdelay = 3; 
    maincar.animcount = 0; 
    maincar.movex = 3; //change this to any value and see the effect 

	//car2 sprite's properties
	car2_image = LoadTexture("car2.png", D3DCOLOR_XRGB(255,0,255));
		if (car2_image == NULL)
        return 0;
	D3DXCreateSprite(d3ddev, &car2_handler);

	for (int n = 0; n<car2num; n++)

	{		

		srand((unsigned int)time(NULL));
		r = rand() % 3 + 1;
		if (r == 1)
		{
			car2[n].x = first;
		}

		if (r == 2)
		{
			car2[n].x = second;
		}

		if (r == 3)
		{
			car2[n].x = third;
		}


		car2[n].y = 0;
		car2[n].width = 84;  //location in the image file
		car2[n].height = 148;  //location in the image file
		car2[n].curframe = 0;  
		car2[n].lastframe = 1;  //12 
		car2[n].animdelay = 3; 
		car2[n].animcount = 0; 
		car2[n].movey = 1; //change this to any value and see the effect 
	}
   

    //return okay
    return 1;
}

//the main game loop
void Game_Run(HWND hwnd)
{
    //make sure the Direct3D device is valid
    if (d3ddev == NULL)
        return;


        //after short delay, ready for next frame?
        //this keeps the game running at a steady frame rate
        if (GetTickCount() - start >= 30)  //big value slow animation effect
        {
            //reset timing
            start = GetTickCount();




            //has animation delay reached threshold?
            if (++maincar.animcount > maincar.animdelay)
            {
                //reset counter
                maincar.animcount = 0;

                //animate the sprite
               
            }

			
			//start of logics about movement
			

			
			if (spawn == true)
			{
				car2[0].y += 2;
				car2[1].y += 4;
				car2[2].y += 5;
				car2[3].y += 6;
				car2[4].y += 7;
				
			}
			

			//end of logics
       }

        //start rendering
        if (d3ddev->BeginScene())
		{
			introduction();
			if (StartGame == true)
			{
				startGame();
				
				car2start();
				
				
				
			}

			
			
			
					
				

            
            //erase the entire background
          

        //stop rendering
        d3ddev->EndScene();
    }

    //display the back buffer on the screen
    d3ddev->Present(NULL, NULL, NULL, NULL);

    //check for escape key (to exit program)
    if (KEY_DOWN(VK_ESCAPE))
        PostMessage(hwnd, WM_DESTROY, 0, 0);


	if(maincar.x > 150) 
	{
		if(KEY_DOWN(VK_LEFT))
		{
			maincar.x -= maincar.movex;
			
		}
	}

	//Moves the truck to the right
	if(maincar.x < (650 - maincar.width))
	{
		if(KEY_DOWN(VK_RIGHT)) 
		{
			maincar.x += maincar.movex;
			
		}
	}

	





}

//frees memory and cleans up before the game ends
void Game_End(HWND hwnd)
{

    if (maincar_image != NULL)
        maincar_image->Release();

    if (back != NULL)
        back->Release();

	if (intro != NULL)
        intro->Release();

    if (sprite_handler != NULL)
        sprite_handler->Release();

	 if (car2_handler != NULL)
        car2_handler->Release();
}
void introduction()
{

	d3ddev->StretchRect(intro, NULL, backbuffer, NULL, D3DTEXF_NONE);	


	if (KEY_DOWN(VK_SPACE))
	
	{
		StartGame = true;
		
		
	}


	
	
	
	
}

void startGame()
{
	
		
			d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);	
			//start sprite handler
			

            sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
            //create vector to update sprite position
	        D3DXVECTOR3 position((float)maincar.x, (float)maincar.y, 0);

            //configure the rect for the source tile
            RECT srcRect;
            int columns = 8;                                                 // next frame 50, 0 and 100 64  
            srcRect.left = (maincar.curframe % columns) * maincar.width;	//left = 1 % 8 * 50 = 50
            srcRect.top = (maincar.curframe / columns) * maincar.height;	//top = 1 / 8 * 64 = 0
            srcRect.right = srcRect.left + maincar.width;					//right = 50 + 50 = 100
	        srcRect.bottom = srcRect.top + maincar.height;					//bottom = 0 + 64

            //draw the sprite maincar
            sprite_handler->Draw(
                maincar_image, 
                &srcRect,
                NULL,
                &position,
                //D3DCOLOR_XRGB(0,0,0));
				D3DCOLOR_ARGB(255, 255, 255, 255)); //you can adjust the alpha coordinate
            
            //stop drawing
            sprite_handler->End();



			
			
			
			

			
}

void car2start()
{
	spawn = true;

	 D3DXVECTOR3 car2position(0,0,0);   //create vector to update sprite position
		

			srand((unsigned int)time(NULL));

			car2_handler->Begin(D3DXSPRITE_ALPHABLEND);

          
	       

            //configure the rect for the source tile

			for (int n = 0; n <car2num; n++)
			{
			RECT car2rect;
            int carcolumn = 8;                                                 // next frame 50, 0 and 100 64  
            car2rect.left = (car2[n].curframe % carcolumn) * car2[n].width;	//left = 1 % 8 * 50 = 50
            car2rect.top = (car2[n].curframe / carcolumn) * car2[n].height;	//top = 1 / 8 * 64 = 0
            car2rect.right = car2rect.left + maincar.width;					//right = 50 + 50 = 100
	        car2rect.bottom = car2rect.top + maincar.height;					//bottom = 0 + 64
			}
            

            

			
				
				timer.run();
					if (timer.seconds % 2 == 1)
					{
				for (int i = 0; i < car2num; i++)

				//draw the sprite
				car2position.x = (float)car2[1].x;
				car2position.y = (float)car2[1].y;		
				car2_handler->Draw(
                car2_image, 
                NULL,
                NULL,
                &car2position,
                //D3DCOLOR_XRGB(0,0,0));
				D3DCOLOR_ARGB(255, 255, 255, 255)); //you can adjust the alpha coordinate
					}
            
			
			//stop drawing
            car2_handler->End();
}
Last edited on
First of all use the [code]-tag to represent code.

I don't know what you mean with "you want to spawn car2 every 2 seonds".

Do you want to have a new car all 2 seconds or do you want to delete the car2 and create a new one each 2 seconds?

@Gamer2015

i want to have a new car per 2 seconds , i dont want to delete the previous car but instead it will go out of the screen as it downwards.

sorry, first time to use this cplusplus.com :))
my target interval for spawn of car2 is every 2 seconds.
and in my code, it draws only the car2 every 2 seconds, its like blinking every 2 seconds ;(

help plsss
yup, thats what it does at the moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
D3DXVECTOR3 car2position(0,0,0);   //create vector to update sprite position
		
srand((unsigned int)time(NULL)); // try to avoid using srand more than once in a programm, put it on the start of the main function

car2_handler->Begin(D3DXSPRITE_ALPHABLEND);

//configure the rect for the source tile

for (int n = 0; n <car2num; n++)
{
    RECT car2rect; // creates car2rect
    int carcolumn = 8;                                                 // next frame 50, 0 and 100 64  
    car2rect.left = (car2[n].curframe % carcolumn) * car2[n].width;	//left = 1 % 8 * 50 = 50
    car2rect.top = (car2[n].curframe / carcolumn) * car2[n].height;	//top = 1 / 8 * 64 = 0
    car2rect.right = car2rect.left + maincar.width;					//right = 50 + 50 = 100
    car2rect.bottom = car2rect.top + maincar.height;					//bottom = 0 + 64
} // car2rect goes out of scope
            
timer.run();
if (timer.seconds % 2 == 1) // i think that means all 2 seconds
{
    for (int i = 0; i < car2num; i++)
    { // added brace here
	    //draw the sprite
            car2position.x = (float)car2[i].x; // changed 1 to i
	    car2position.y = (float)car2[i].y; // changed 1 to i	
	    car2_handler->Draw( /// draw the car2 image on car2position
                car2_image, 
                NULL,
                NULL,
                &car2position,
                //D3DCOLOR_XRGB(0,0,0));
                D3DCOLOR_ARGB(255, 255, 255, 255)); //you can adjust the alpha coordinate
    } // added brace here
}
            
//stop drawing
car2_handler->End();


try again.

It's quite hard to go over your code, but replacing the 1 with i when setting the car positon should do the trick, otherwise you have an other problem^^
@Gamer2015 I tried that, but still its like blinking. for every 2 seconds, and all of the array of cars[n] which is 5, falls at the same time. What i mean is , all five car2s are showing up in the window and blinks for every 2 seconds omgg -.- i need you man.
Topic archived. No new replies allowed.