Problem with scheduling

Good evening people, being the newibie i am, i’m facing a problem with scheduling and i can’t seem to be able to figure what it is. here’s the thing. i’m trying to spawn sprites in the right of the screen and as soon as one leaves the screen from the left, another one spawns. let me show you the snippets

this is the header file. forget that you saw the implementations

#include “cocos2d.h”

using namespace cocos2d;

class SpawnLitter : public Sprite
{
public:

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 endPos = Vec2(0, visibleSize.height/2);

Sprite* litter;

void spawnLitter(Sprite* sprite)
{
	sprite = Sprite::create("8ball.png");
	sprite->setPosition(Vec2(visibleSize.width * 1.5, visibleSize.height / 2));
	this->addChild(sprite, 1);

}

void moveLitter()
{
	spawnLitter(litter);
	Action* action = MoveBy::create(5, Vec2(-visibleSize.width + 1, 0));
	litter->runAction(action);

	/*if (litter->getPosition().x < endPos.x)
	{
		litter->autorelease();
	}*/
	
}

};

this is the gameplayscene method i’m trying to schedule and gives me a return texture error

void GamePlayScene::SpawnLitter(float dt)
{
litter_1.moveLitter();
CCLOG(“Litter Spawned”);

}

litter_1 is aSpawnLitter class object…any help would be appreciated…go easy i’m a noob.

Hey have you written any update functions?

Write an update function and call it using scheduler.
Haha, this is what you are asking.

To see usage. I would suggest using cocos2d-x sample test. Run them. Find the similar things that you need to implement.
See the code in Classes file of these sample tests and enjoy…
As far as I rem. there is some test name availalbe with name ‘Scheduler’ which you can see after running the sample test. And do run these tests in real device

I’ve written an update function yes, and it is called via ScheduleUpdate()…
the game starts and seconds later crashes, directing me to return_texture when crashing…and the output window says access violation…

maybe the problem derives from the sprite i created inside the SpawnLitter class, and the fact that i used the “this” operator inside the class? i dunno…i’m trying really hard to grasp the way cocos2dx works…

Can you tell what exactly your error report says. Check console/logcat

And also, how are you calling this ScheduleUpdate() function.
Hope you’ve put some scheduler for calling this

Also, I didn’t understand by your line:

Or if possible can you give your actual code snippet!!

and the code snippet for update - ScheduleUpdate

HI,

I think the error is saying that the Sprite* litter is not instantiated properly. Check that you can actually create a sprite with your "8ball.png".

Also, refactor your code. It looks messy. You have SpawnLitter that inherits from Sprite but you are creating another sprite as a child with your spawnLitter function. Is your SpawnLitter some kind of a litter manager where it decides when and where to spawn the litter? Does it exist in your scene visually? Do you interact with it?

Ya… as even @jonah1nine
said, that you need to refactor your code.

Apart from this, my observation on your code are following and you could try the following things.

  1. Try instantiating sprite *litter to nullptr

  2. If the 2 doesnot work, try to put

  3. If I am not mistaken, are you realizing that the each time you’re calling moveLitter, it is adding it to the layer using
    this->addChild(sprite, 1);
    And what is your this in the statement this->addChild(sprite, 1);

  4. So, if you’re trying to create a sprite using one function and if you’ve trying to make other function for its movement, then its good approach but the way you’ve implemented is not good.

I seriously cannot add to this further as I don’t know how this class is being used.

Also, otherwise your scheduling is perfectly correct. Also, if you are ok with the rate, with which your GamePlayScene::update function is being updated and you may want to increase the rate of scheduleUpdate() then put this inside your GamePlayScene::update method and don’t schedule scheduleUpdate() . I mean Update() will take care of calling it again. If you don’t want this… Then its fine with what you’ve implemented for scheduling.

More additions to your code…

In my last post, by 4) point I mean,
don’t add spawnLitter(litter); in the void moveLitter()
because I think you’re trying to just move an already existing sprite so why are you creating a sprite again in this method.

I think by the your ‘topic of this post’, I understand that… you know concepts about creating a game without using a framework… So, that is why you’re confusing with the update scheduler.

If I am correct, is something similar that is given below that you are trying to do …(I’ve written a pseudo Code of a function that is getting updated regularly)
If yes, then please ask to explain what is scheduler and update is used for.


Animation(ball, player, time,…)
{
latestTime=getLatestTime();
timeDT= latestTime-time

clearAllSpritesOnScreen();

updateBallPositionl(ball,timeDT); // get the latest position of the ball
drawBall(ball,ball.x,ball.y); // draw ball on the screen with its latest position

updatePlayerPosition(player,timeDT); // get the latest position of the player
drawPlayer(player, player.x,player.y); // draws player on the screen with its latest position

RegularScheduleMe(callAnimation(ball, player, latestTme));
}


@GeorgeDev

If I understood correctly, then here is what I understood, that your spawnLitter is just for creating as many balls
and moveLitter is just to move the sprite.

Use the below Code:
Note: I am not seeing whether you’ve implemented action, moveBy correctly, I am just writing the code according to the scheduling and calling.


include “cocos2d.h”
using namespace cocos2d;
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 endPos = Vec2(0, visibleSize.height/2);

class SpawnLitter : public Sprite
{
public:
Public:
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 endPos = Vec2(0, visibleSize.height/2);

void spawnLitter(Sprite* sprite, Node *currentObject) // Should be used if you want to create a new such sprite!!

{
sprite = Sprite::create(“8ball.png”);
sprite->setPosition(Vec2(visibleSize.width * 1.5, visibleSize.height / 2));
curentObject->addChild(sprite, 1);

}

moveLitter(Sprite *litter_1)
{
Sprite *sprite=litter_1;

Action* action = MoveBy::create(5, Vec2(-visibleSize.width + 1, 0));
sprite->runAction(action);

}


So somewhere is your game code where you’re spawning sprites, you must do below:


spawnLitter(litter_1,this);// “this” is the place where you’re calling it
moveLitter(litter_1);


And rem.

The problem specific to your code that you submitted in your first post is:

*YOUR consutrcutor spawnLitter(Sprite sprite)

  1. You might be doing this:
    SpawnLitter litter1_ = new SpawnLitter();
    // Creating a object of this class and also calling the constructor at the same time…
    You’ve not submitted us this code, so this is possible problem:
    You’ve might not have passed any sprite(which is of type Sprite) because your constructor needs that.

  2. Now, suppose you’ve passed correct sprite and instead of the above, suppose you would have done this:

Sprite *sp = Sprite:create(); // which is a sprite with no texture;
SpawnLitter litter1_ = new SpawnLitter(sp);

Now, your void spawnLitter(Sprite *sprite) constructor is trying to do following:

  1. add texture(ball.png) to your original sprite sp.
    2)Adding position to it.
  2. PROBLEM IS IN THIS STATEMENT, as it doesnot know what is “this”
    you must pass, a Node(target) while creating an object litter_1.

YOUR void moveLitter()
When you do litter_1.moveLitter(),
while you’re spawning using spawnLitter(litter);… how would it know where to add… I mean this->addChild(sprite,1) doesnot know what is this here, so pass a Node in litter_1.moveLitter();

This is the error in your code…
One Note for you, please attach a certain significant piece of code, Otherwise, it becomes difficult to understand what you are trying to do/ Like you’ve missed how you’re creating litter_1 object

AND LASTLY
if you’re trying to achieve

Then your moveLitter() is wronly implemented. Infact, I would say forget this code and try to write it different way… As this is not a correct approach of doing what you’re trying to do.

Hope that helps

thank you very much for taking the time to help me, forgive me if i left important code parts out, here let me send you the files of this prototype.files.zip (4.7 KB) .
I’ll work on your suggestions and post back…

I’ll get back to you soon… after seeing errors in my own IDE :smile: