-1

Im making a small boxing app. This whole week iv been trying to work out how to solve my current problem above, with no luck :(.

Summary

Basically Its a 1 v 1 game. Each time the user hits the other player I want to make a tiny number pop up near him and float up and dissappear.

Its sort of like when you play MMORPG's and when you do dmg you see how much you did. See image below for an example!

Problem

So basically everytime the user hits the other player I want a little number to pop up on the screen to show the dmg and then float up and disappear.

Details

  1. I am building the game on a simple UIView
  2. The label is a UILabel

Anywhere How I can achieve this?

Thank you!

brkr
  • 834
  • 8
  • 18
  • You are not sharing crucial pieces of information with those who read your topic. (1) How do you develop your game? Are you using SpriteKit, SceneKit or just UIView? (2) What is custom label? Is it an instance SKLabel, UILabel or NSTextField? So far, you are getting an F in the way you ask a question at a web site open to the public. – El Tomato May 24 '16 at 22:04

1 Answers1

1

Create a label, then use UIView.animateWithDuration to animate it.

    let label = UILabel(frame: CGRect(origin: point/*The point where you want to add your label*/, size: CGSize(width: 50, height: 50)))
    label.text = "+1"
    label.font = UIFont()//Your font
    label.textColor = UIColor.blueColor()
    label.alpha = 1
    self.view.addSubview(label)

    UIView.animateWithDuration(1) { 
        label.center = CGPoint()//The point where you want your label to end up
        label.alpha = 0
    }

Edit: As mentioned in the comments, you asked for how to create the label at a random point. Try this:

    let screenWidth = self.view.frame.width
    let screenHeight = self.view.frame.height

    let randomX = CGFloat(arc4random_uniform(UInt32(screenWidth)))
    let randomY = CGFloat(arc4random_uniform(UInt32(screenHeight)))

    let point = CGPoint(x: randomX, y: randomY)

As you will notice, for the width and height, I use the view's frame's width and height. You may want to use the view's bounds. For more on this, check out this SO Post.

Community
  • 1
  • 1
Pranav Wadhwa
  • 7,202
  • 6
  • 31
  • 53
  • Dang, you are a genius! What if i want the origin `point` to be randomly somewhere in the view? – brkr May 24 '16 at 22:27
  • Awesome it works 100% :). By any chance would you know how I can use NSTimer to monitor if a variable gets updated? Because on my game if they stop clicking I want it to get reset back to +1. For example If `var pts = 1324` doesnt change in 5 seconds, make `var addPTS = +1`? – brkr May 25 '16 at 00:45
  • @brkr You don't need to use an NSTimer for this. Try [delaying your code with this SO Post](http://stackoverflow.com/a/24318861/5143847) – Pranav Wadhwa May 25 '16 at 01:00