I am testing with the appscaffold and I have an issue removing an SPImage
so far this is what I have
game.h
--------------------------------------
@interface Game : SPStage
{
SPImage *image1;
}
-(void)runLoop:(SPEvent *)event;
-(void)stopanimation:(SPEvent *)event;
@end
--------------------------------------
game.m
--------------------------------------
- (id)initWithWidth:(float)width height:(float)height
{
if ((self = [super initWithWidth:width height:height]))
{
image1 = [SPImage imageWithContentsOfFile:@"bubble.png"];
[self addchild:image1];
[self addEventListener:@selector(runLoop:) atObject:self forType:(SP_EVENT_TYPE_ENTER_FRAME)];
[self addEventListener:@selector(stopanimation:) atObject:self forType:(SP_EVENT_TYPE_ENTER_FRAME)];
}
return self;
}
-(void)runLoop:(SPEvent *)event
{
double scrollSp = 1;
image1.y = image1.y + scrollSp;
}
-(void)stopanimation:(SPEvent *)event
{
if(image1.y == 300)
{
[self removeEventListener:@selector(runLoop:) atObject:self forType:(SP_EVENT_TYPE_ENTER_FRAME)];
[self removeChild:image1];
}
}
---------------------------------------
I have a problem in the stop animation
I am trying to make the image scroll across the screen and when it reaches a certain point i want to remove it.
but when I run it i get a problem. I think its the line where i try to remove the image1
[self removeChild:image1];
without it it works fine and the bubble stops scrolling once it reaches 300.
but when i add the above line my program crashes. Even when I use
[image1 removeFromParent];
it gives me the same kind of error
the error I get is an exc_bad_access here
- (void)dispatchEvent:(SPEvent*)event
{
NSMutableArray *listeners = [mEventListeners objectForKey:event.type];
if (!event.bubbles && !listeners) return; // no need to do anything.
// if the event already has a current target, it was re-dispatched by user -> we change the
// target to 'self' for now, but undo that later on (instead of creating a copy, which could
// lead to the creation of a huge amount of objects).
SPEventDispatcher *previousTarget = event.target;
if (!event.target || event.currentTarget) event.target = self;
event.currentTarget = self;
[self retain]; // the event listener could release 'self', so we have to make sure that it
// stays valid while we're here.
BOOL stopImmediatPropagation = NO;
if (listeners.count != 0)
{
// we can enumerate directly over the array, since "add"- and "removeEventListener" won't
// change it, but instead always create a new array.
[listeners retain];
for (NSInvocation *inv in listeners)
{
[inv setArgument:&event atIndex:2];
->->->->->->[inv invoke];<-<-<-<-<-<-
I dont understand why i get an error at
[inv invoke];
furthermore I want to understand how removeChild works
since i allocated the SPImage image1 with an image, when i do remove child
is the image released does it go away or do i have to call the image1 release function.
I tried doing that before but it gave me another error
- (void)render:(SPRenderSupport *)support
{
[SPRenderSupport clearWithColor:mColor alpha:1.0f];
[SPRenderSupport setupOrthographicRenderingWithLeft:0 right:mWidth bottom:mHeight top:0];
->->[super render:support];
I get a exc_bad_access error at the line above.