Object in my game is moving(it's coordinates are changing), but I can't see it.
My object is like frozen in 1 position.
Screen doesn't refresh.
How do I refresh the screen?
Thanks!
How to refresh screen?
(13 posts) (5 voices)-
Posted 1 year ago #
-
Maybe you should show us the moving code..
Posted 1 year ago # -
I have the timer and every second I change the position.
This is how I add my sprite:
CCSprite* player = [CCSprite spriteWithFile:@"pl.png"];
player.position=ccp(plx,ply) ;
[self addChild: player];plx and ply are floats
plxv and plyv are floats too-(void) updateMeter:(NSTimer *)aTimer{
time=-[start timeIntervalSinceNow];
ply-=plyv;//NSLog(@"%f",ply);
}plyv is velosity.
plyv -1;Please help!
Thanks!Posted 1 year ago # -
Well I don't really see the part you're changing the player's position, you're just working with some variables...
Plus I heard that NSTimer was not a very good idea, instead use a schedule, like in the init method, put this:
[self schedule:@selector(update:) interval:1.0];
Which will call this function once per second:
-(void)update:(ccTime)dtPosted 1 year ago # -
It work's the same, but my sprite isn't moving at all.
Like it was before.
Does anyone have an idea how to refresh/reload layerPosted 1 year ago # -
It looks like you're changing the variable name ply in the updateMeter, but it doesn't appear you are actually applying it to the player CCSprite. Changing the variable you passed in the original creation of the sprite does not automatically move it.
You will need to call [player setPosition:ccp(plx,ply)] after you update ply.
It isn't clear from your code sample, but you probably want to (if you haven't already) establish your player as an iVar, so you have access to it after it is created. (If you haven't, then you have no direct control over the sprite once it is added)
Edited to add: there really isn't a "refresh", since the display is redrawn every cycle (usually 60 frames per second). Once you move an object's position, it should move.
Posted 1 year ago # -
This is all the code that I use:
// Import the interfaces
#import "HelloWorldScene.h"float plx=512;
float ply=500;
float plxv=0;
float plyv=1;// HelloWorld implementation
@implementation HelloWorld+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];// add layer as a child to scene
[scene addChild: layer];// return the scene
return scene;
}// on "init" you need to initialize your instance
-(id) init
{
[self schedule:@selector(update:) interval:0.01];
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {CCSprite* player = [CCSprite spriteWithFile:@"pl.png"];
player.position=ccp(plx,ply) ;
[self addChild: player];CCSprite* platform1 = [CCSprite spriteWithFile:@"platform.png"];
platform1.position=ccp(-100,100);
[self addChild: platform1];CCSprite* platform2 = [CCSprite spriteWithFile:@"platform.png"];
platform2.position=ccp(400,150);
[self addChild: platform2];CCSprite* platform3 = [CCSprite spriteWithFile:@"platform.png"];
platform3.position=ccp(900,200);
[self addChild: platform3];}
return self;
}-(void)update:(ccTime)dt{
ply-=plyv;
NSLog(@"%f",ply);
}// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)// don't forget to call "super dealloc"
[super dealloc];
}
@endvariables are changing but layer doesn't refresh.
Does anyone have idea how to refresh the screen?Posted 1 year ago # -
We told you you're just changing a variable, which is totally different than changing a sprite's position.
In the update function, change the sprite position like
player.position = ccp(x,y)Posted 1 year ago # -
As Dragyn said you need to update the sprite. Not a good idea to assign the sprite as a local variable and no tag was assigned.
Put
CCSprite* player;
in your .h fileand change
CCSprite* player = [CCSprite spriteWithFile:@"pl.png"];
to
player = [CCSprite spriteWithFile:@"pl.png"];Then after
ply-=plyv;
put
[player setPosition:ccp(plx,ply)]That should work now
Also probably best to perhaps put the variables also in you .h file.
Posted 1 year ago # -
You code is not changing sprite position. It only updates the variable value (ply). Changing ply value will not automatically update player position.
Posted 1 year ago # -
But how do I update sprite's position?
Posted 1 year ago # -
player.position = ccp(x,y)
Posted 1 year ago # -
It works! Thank you!
Posted 1 year ago #
Reply
You must log in to post.