So im writing my first lines of code in Swift, and i want to make a game where you need to tilt the device to make a gravity shift and by doing so make a ball drop to the floor while maneuvering around obstacles.
Ive been looking up different methods, and I found one where you can make the obstacles by just using let, For example (Im using UIKit)
let barrier1 = UIView(frame: CGRect(x: 0, y: 300, width: 20, height: 20))
barrier1.backgroundColor = UIColor.redColor()
view.addSubview(barrier1)
Its quite a bit of code to declare every barrier this way, and I was hoping if there was a way of doing this in a while loop where every time the loop runs, x = x + 1 is stated and you make variables that way in batch. Can anyone help me? Thanks!
This is what arrays are for. Declare a variable called barriers of type [UIView] and then append each new barrier to this array.
var barriers:[UIView] = []
for i in 1...20 {
let newbarrier = UIView(frame: CGRect(x: 0, y: 300 + i * 30, width: 20, height: 20))
newbarrier.backgroundColor = UIColor.redColor()
view.addSubview(newbarrier)
barriers.append(newbarrier)
}
// Now you can refer to each barrier by its index: barriers[0], barriers[1]
// up to barriers[19]
Related
I have an idea for AR app, and I noticed that in most AR apps the objects are not hided through the wall, is it possible for example I pinned the AR portrait in my room, and it is only can be seen if I go there, not through the walls?
I have no experience in AR, just about to learn it.
Thank you.
it's simple way to do this, you have to detect plane (in your case a wall) and setup for
node.geometry.firstMaterial?.colorBufferWriteMask = []
node.renderingOrder = -1
You can easily hide all your objects behind a wall using three different approaches for wall creation: SCNBox(), SCNShape() with extrusion, or SCNGeometry(). Whatever you choose, just assign empty instance property .colorBufferWriteMask to it. And .renderingOrder instance property must be -1. Node with a negative value of rendering order is rendered first.
let wallNode = SCNNode()
wallNode.geometry = SCNBox(width: 15.0, height: 3.0, length: 0.1, chamferRadius: 0)
wallNode.position = SCNVector3(x: 0, y: 0, z: 5)
//wallNode.geometry = SCNShape(path: NSBezierPath?, extrusionDepth: CGFloat)
//wallNode.geometry = SCNGeometry(sources: [SCNGeometrySource], elements: [SCNGeometryElement]?)
wallNode.geometry?.firstMaterial?.colorBufferWriteMask = []
wallNode.renderingOrder = -1
scene.rootNode.addChildNode(wallNode)
Hope this helps.
I am trying to make a basic game for iOS10 using swift 3 and scenekit. In one part of my games code I have a function that adds fishes to the screen, and gives each one a certain tag so i can find them later:
let fish = UIImageView(frame: CGRect(x: 0, y: 0, width: CGFloat(fishsize.0), height: CGFloat(fishsize.1)))
fish.contentMode = .scaleAspectFit
fish.center = CGPoint(x: CGFloat(fishsize.0) * -0.6, y: pos)
fish.animationImages = fImg(Fishes[j].size, front: Fishes[j].front)
fish.animationDuration = 0.7
fish.startAnimating()
fish.tag = j + 200
self.view.insertSubview(fish, belowSubview: big1)
What I would like is to be able to, at a certain point, recall the fish and
Change the images shown, and
Stop the animation.
Is this possible? I've been trying it with var fish = view.viewWithTag(killer+200)! but from this I can't seem to change any image properties of the new variable fish.
Any help would be much appreciated.
Tom
Try to cast the UIView to UIImageView like this.
if let fish = view.viewWithTag(killer+200) as? UIImageView {
//Perform your action on imageView object
}
local rect = display.newRoundedRect(200, 200, 150, 150, 2)
rect.fill = {0,0,0}
rect.alpha = 0.1
rect.xScale = 0.1
rect.yScale = 0.1
transition.to(rect, {time=1000, xScale=1, yScale=1, alpha=1}) -- transA
local function moveListener()
transition.moveTo(rect, {time=800,x=300, y=300}) -- this will puase transA!!
end
timer.performWithDelay(400,moveListener, 1)
Any idea how to perform moveTo() without stopping other transitions?
Cheers!
I just tried this on my computer, and it seems to be an issue with transition.moveTo().
It works using transition.to().
I just tried the following:
local rect1 = display.newRoundedRect(100, 100, 150, 150, 2)
rect1.fill = {0,0,0}
local rect2 = display.newRoundedRect(300, 100, 150, 150, 2)
rect2.fill = {1,0,1}
transition.moveTo(rect1, {time=1000, y=500})
transition.to(rect2, {time=1000, y=500})
And noticed that rect1 moved after rect2 with some delay!. However, adding small delay to transition.to() will make the movement exactly identical as follows:
transition.to(rect2, {delay=1, time=1000, y=500})
To summerize, it is still an issue with moveTo() to stop other transitions. As work around, in order to get exact behavior just use transition.to() with micro delay as previous line of code!
Thanks.....
For an App I'm making i need to use variables to change the size and position of objects (Labels). I've tried var example = CGRectMake(0, 0, 0, 100), hoping it would ignore the zeros (Not really thinking it would though). I then tried:
var example = 100
Label1.frame = CGRectMake(20, 20, 50, example)
I changed the syntax a bit, adding "" and replacing the CGRectMake with CGRect etc, but nothing worked... I don't get what I'm doing wrong here... Help!
Below is the new syntax used since Swift 3.
CGRect(x: 0, y: 0, width: 100, height: 100)
CGRectMake takes CGFloats for all of its arguments. Your sample code should work fine if you specify that example is supposed to be a CGFloat, using a type identifier:
// v~~~~ add this...
var example: CGFloat = 100
Label1.frame = CGRectMake(20, 20, 50, example)
Otherwise, swift infers the type of example to be Int, and the call to CGRectMake fails, cuz it can't take an Int as a parameter...
So, there is many ways to skin the cat. It all depends what your needs and requirements are (maybe you could elaborate a bit on what you are trying to achieve?). But one way to do it could be to set a variable when something happens, and then update the frame of the label. If you added a tap gesture recognizer to your view, and updated your label like so:
let myLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
let tapGestRecog = UITapGestureRecognizer(target: self, action: "handleTap:")
self.view.addGestureRecognizer(tapGestRecog)
}
func handleTap(sender:UIGestureRecognizer) {
let newXposition = sender.locationInView(self.view).x
let newYposition = sender.locationInView(self.view).y
myLabel.frame = CGRectMake(newXposition, newYposition, 200, 200)
}
This is just an example, and a very crude way of doing it. There are many other ways of doing it, but it hopefully gives you an idea of how to achieve it.
Swift allows syntax that Objective-C does not:
var example = 100
label.frame.size.height = example
In objective-C you would have to do it differently:
CGRect frame = label.frame; //Create a temporary rect to hold the frame value
frame.size.height = example;
label.frame = frame;
So I have my scene presenting and am trying to display a sprite when I press a button. I know the function is called because of a NSLog but I can't get the sprite to show.
func ShowShip() {
var booster = CCBReader.load("ccbResources/Booster")
booster.position = CGPoint(x: 0, y: 0)
self.addChild(booster)
NSLog("created sprite")
}
The log is called but the sprite isn't displayed. I looked at the quickstart tutorial and couldn't see much difference.
edit: tried calling the .png resource directly but got unwrapping error
Try direct method:
//method_1 : read image from disk
var booster = CCSprite(imageNamed:"Booster.png")
booster.position = CGPoint(x: 50, y: 50)
self.addChild(booster, z:3)
//method_2 : read image from sprite sheet
var frame1 = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("Booster.png") as CCSpriteFrame
var booster = CCSprite(spriteFrame: frame1)
booster.position = CGPoint(x: 50, y: 50)
self.addChild(booster, z:3)