I have a ball for which I have different colors. The ball has two textures(texture1 and texture2) that I run to do an animation on them. I have different colored balls. Once I call a random function to call any random ball(texture1), my other function doesn't match it with the same ball color(texture2). For instance, if my texture1
calls redBallUp, my other function doesn't match it with redballDown
, but randomly calls a texture. Sometimes, it matches it up, but the animation keeps switching between different textures, and sometimes, it doesn't call texture2 at all.
Here's how I set it up:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let ballTexture1 = SKTexture(imageNamed: getRandomBallUp())
ballTexture1.filteringMode = SKTextureFilteringMode.Nearest
let ballTexture2 = SKTexture(imageNamed: getRandomBalldDown())
ballTexture2.filteringMode = SKTextureFilteringMode.Nearest
let anim = SKAction.animateWithTextures([ballTexture1, ballTexture2], timePerFrame: 0.15)
let upDown = SKAction.repeatActionForever(anim)
ball = SKSpriteNode(texture: ballTexture1)
ball.position = CGPoint(x: self.frame.size.width / 2.8, y:CGRectGetMidY(self.frame))
ball.runAction(upDown)
self.addChild(ball)
}
func getRandomBallUp() -> String{
let randomval = arc4random_uniform(6)
var ballUp = ""
switch(randomval)
{
case 0:
ballUp = "BlueBallUp"
case 1:
ballUp = "DarkBlueBallUp"
case 2:
ballUp = "GreenBallUp"
case 3:
ballUp = "PurpleBallUp"
case 4:
ballUp = "RedBallUp"
case 5:
ballUp = "YellowBallUp"
default:()
}
return ballUp
}
func getRandomBallDown() -> String{
var ballDown = ""
if (getRandomBallUp() == "BlueBallUp") {
ballDown = "BlueBallDown"
}
if (getRandomBallUp() == "DarkBlueBallUp") {
ballDown = "DarkBlueBallDown"
}
if (getRandomBallUp() == "GreenBallUp") {
ballDown = "GreenBallDown"
}
if (getRandomBallUp() == "PurpleBallUp") {
ballDown = "PurpleBallDown"
}
if (getRandomBallUp() == "RedBallUp") {
ballDown = "RedBallDown"
}
else if (getRandomBallUp() == "YellowBallUp") {
ballDown = "YellowBallDown"
}
return ballDown
}
答案 0 :(得分:1)
Every time you call getRandomBallUp() you are generating a random value and returning a string based on that value. To avoid this, uou need to change your echo image_thumb( 'assets/images/picture-1/picture-1.jpg', 50, 50 );
to something like:
getRandomBallDown()
You need to also change your didMoveToView:
func getCorrespondingRandomBallDown(ballUp:String) -> String{
var ballDown = ""
if (ballUp == "BlueBallUp") {
ballDown = "BlueBallDown"
}
if (ballUp == "DarkBlueBallUp") {
ballDown = "DarkBlueBallDown"
}
if (getRandomBallUp() == "GreenBallUp") {
ballDown = "GreenBallDown"
}
if (ballUp == "PurpleBallUp") {
ballDown = "PurpleBallDown"
}
if (ballUp == "RedBallUp") {
ballDown = "RedBallDown"
}
else if (ballUp == "YellowBallUp") {
ballDown = "YellowBallDown"
}
return ballDown
}
答案 1 :(得分:1)
func getRandomBall() -> (up:String,down:String){
let randomval = arc4random_uniform(6)
var ball = ""
switch(randomval)
{
case 0:
ball = "BlueBall"
case 1:
ball = "DarkBlueBall"
case 2:
ball = "GreenBall"
case 3:
ball = "PurpleBall"
case 4:
ball = "RedBall"
case 5:
ball = "YellowBall"
default:()
}
return (up:"\(ball)Up",down:"\(ball)Down")
}
override func didMoveToView(view: SKView) {
let ballTextureName = getRandomBall()
let ballTexture1 = SKTexture(imageNamed: ballTextureName.up)
ballTexture1.filteringMode = SKTextureFilteringMode.Nearest
let ballTexture2 = SKTexture(imageNamed: ballTextureName.down)
ballTexture2.filteringMode = SKTextureFilteringMode.Nearest
let anim = SKAction.animateWithTextures([ballTexture1, ballTexture2], timePerFrame: 0.15)
let upDown = SKAction.repeatActionForever(anim)
ball = SKSpriteNode(texture: ballTexture1)
ball.position = CGPoint(x: self.frame.size.width / 2.8, y:CGRectGetMidY(self.frame))
ball.runAction(upDown)
self.addChild(ball)
}