user5127227
user5127227

Reputation:

set UIImageView at the right side off the screen

I have a UIImageView called ufo. It is moving left outside the screen till you can't see it anymore and I want it to respawn at the right side off the screen and move in . The left side is working but the right side isn't.

if (ufo.center.x < (-ufo.frame.size.width/2)) {
            ufo.center = CGPointMake((backGround.frame.size.width -    (ufo.frame.size.width / 2)), ufo.center.y);
        }

It is respawning completely at the right side, not coming off the screen. I know there should be a + in the CGPointMake, but then it is bugging at the left side!

Can somebody help?

Thank you.

Upvotes: 1

Views: 119

Answers (2)

Unheilig
Unheilig

Reputation: 16292

I would have done something like the following as per your criteria:

if (ufo.center.x < (backGround.frame.origin.x - (ufo.bounds.size.width / 2.0))) 
{
    //just guessing, since you haven't shown your animation code, but, add the following line:
    [ufo.layer removeAllAnimations];
    //you haven't shown enough, so here is another shot in the dark:
    [timer invalidate];
    ufo.center = CGPointMake((backGround.frame.size.width + (ufo.bounds.size.width / 2.0)), ufo.center.y);
}

The following serves to mimic the behavior of your game, based upon some guessing (since you haven't shown enough) and the information you have thus far provided.

Your UFO is flying on my screen now from right to left and back to the right, based upon your criteria:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createMyUFOandMyBackground];
}

- (void)createMyUFOandMyBackground
{
    myBackground = [[UIImageView alloc] initWithFrame:self.view.bounds];
    myBackground.image = [UIImage imageNamed:@"background"];
    [self.view addSubview:myBackground];

    myUFO = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ufo"]];
    myUFO.center = (CGPoint){myBackground.bounds.size.width + (myUFO.bounds.size.width / 2.0f), myBackground.center.y};
    [self.view addSubview:myUFO];

    [self createTimer];
}

- (void)createTimer
{
    myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(moveUFOToLeft) userInfo:nil repeats:YES];
}

- (void)moveUFOToLeft
{
    temp = 0;
    if (myUFO.center.x < (myBackground.frame.origin.x - (myUFO.bounds.size.width / 2.0)))
    {
        [myTimer invalidate];
        myTimer = nil;
        myUFO.center = CGPointMake((myBackground.frame.size.width + (myUFO.bounds.size.width / 2.0)), myUFO.center.y);
        [self restartMyTimerAfterSeconds];
    }
    else
    {
        temp = - arc4random_uniform(10);
        myUFO.center = CGPointMake(myUFO.center.x + temp, myUFO.center.y);
    }
}

- (void)restartMyTimerAfterSeconds
{
    //This is specific to your game; I will leave that to you.

    [self createTimer];
}

Upvotes: 1

deoKasuhal
deoKasuhal

Reputation: 2897

I am assuming that you added ufo on top of backGround and try to move the ufo from right to left just above the background view with the help of timer.

Use following source code inside the timer method

//Constant which will allow ufo to be moved from right to left    
CGFloat temp = -2;
//Create new point after adding moving offset for ufo
CGPoint point = CGPointMake(ufo.center.x + temp, ufo.center.y);
//Check weather new points is moved away from background if so then assign new center to the right
if ((point.x + CGRectGetWidth(ufo.bounds)/2) < 0.0f) {
            point.x = CGRectGetWidth(ufo.bounds)/2 + CGRectGetMaxX(backGround.bounds)
        }
//Assign the new center to ufo for providing movement from its last position.
ufo.center = point;

Upvotes: 0

Related Questions