生成随机颜色并将其分配给对象

时间:2015-09-01 12:43:42

标签: ios swift uicolor arc4random

我想制作DEBUG ConfigurationFactory - Configuring ehcache from InputStream [MyAPP] 01 Sep 2015 12:36:58 DEBUG CacheManager - Creating new CacheManager with Configuration Object [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - propertiesString is null. [MyAPP] 01 Sep 2015 12:36:58 DEBUG ConfigurationHelper - No CacheManagerEventListenerFactory class specified. Skipping... [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - Value found for hostName: 10.0.110.134 [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - Value found for port: 40001 [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - Value found for remoteObjectPort: null [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - Value found for socketTimeoutMillis: 20000 [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - Value found for peerDiscovery: manual [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - Value found for rmiUrls: //10.0.110.133:40001/sessionIds [MyAPP] 01 Sep 2015 12:36:58 DEBUG RMICacheManagerPeerProviderFactory - Registering peer //10.0.110.133:40001/sessionIds [MyAPP] 01 Sep 2015 12:36:58 DEBUG RMICacheManagerPeerListener - 0 RMICachePeers bound in registry for RMI listener [MyAPP] 01 Sep 2015 12:36:58 DEBUG PropertyUtil - propertiesString is null. [MyAPP] 01 Sep 2015 12:36:58 DEBUG Cache - CacheWriter factory not configured. Skipping... [MyAPP] 01 Sep 2015 12:36:58 DEBUG ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping... [MyAPP] 01 Sep 2015 12:36:58 DEBUG MemoryStore - Initialized net.sf.ehcache.store.NotifyingMemoryStore for sessionIds [MyAPP] 01 Sep 2015 12:36:58 DEBUG Cache - Initialised cache: sessionIds [MyAPP] 01 Sep 2015 12:36:58 DEBUG RMICacheManagerPeerListener - Adding to RMI listener [MyAPP] 01 Sep 2015 12:36:58 DEBUG RMICacheManagerPeerListener - 0 RMICachePeers bound in registry for RMI listener [MyAPP] 01 Sep 2015 12:36:58 DEBUG ConfigurationHelper - CacheDecoratorFactory not configured. Skipping for 'sessionIds'. [MyAPP] 01 Sep 2015 12:36:58 DEBUG ConfigurationHelper - CacheDecoratorFactory not configured for defaultCache. Skipping for 'sessionIds'. [MyAPP] 01 Sep 2015 12:36:58 DEBUG RMIBootstrapCacheLoader - Attempting to acquire cache peers for cache sessionIds to bootstrap from. Will wait up to 0ms for cache to join cluster. [MyAPP] 01 Sep 2015 12:36:58 DEBUG RMICacheManagerPeerProvider - Lookup URL //10.0.110.133:40001/sessionIds [MyAPP] 01 Sep 2015 12:36:58 DEBUG ManualRMICacheManagerPeerProvider - Looking up rmiUrl //10.0.110.133:40001/sessionIds through exception sessionIds. This may be normal if a node has gone offline. Or it may indicate network connectivity difficulties java.rmi.NotBoundException: sessionIds at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:137) at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source) at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267) at sun.rmi.transport.Transport$2.run(Transport.java:202) at sun.rmi.transport.Transport$2.run(Transport.java:199) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:198) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275) at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:378) at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) at java.rmi.Naming.lookup(Naming.java:101) at net.sf.ehcache.distribution.RMICacheManagerPeerProvider.lookupRemoteCachePeer(RMICacheManagerPeerProvider.java:127) at net.sf.ehcache.distribution.ManualRMICacheManagerPeerProvider.listRemoteCachePeers(ManualRMICacheManagerPeerProvider.java:95) at net.sf.ehcache.distribution.RMIBootstrapCacheLoader.listRemoteCachePeers(RMIBootstrapCacheLoader.java:245) at net.sf.ehcache.distribution.RMIBootstrapCacheLoader.acquireCachePeers(RMIBootstrapCacheLoader.java:198) at net.sf.ehcache.distribution.RMIBootstrapCacheLoader.doLoad(RMIBootstrapCacheLoader.java:132) at net.sf.ehcache.distribution.RMIBootstrapCacheLoader$BootstrapThread.run(RMIBootstrapCacheLoader.java:107) [MyAPP] 01 Sep 2015 12:36:59 DEBUG RMIBootstrapCacheLoader - cache peers: [] [MyAPP] 01 Sep 2015 12:36:59 DEBUG RMIBootstrapCacheLoader - Empty list of cache peers for cache sessionIds. No cache peer to bootstrap from. [MyAPP] 01 Sep 2015 12:37:14 DEBUG CacheManager - Attempting to create an existing singleton. Existing singleton returned. 颜色而不是 blackcolor 。 我找到了随机颜色实现,但无法将“Obstacle_Color”与rand方法的返回值合并。提前谢谢!

Obstacle_Color = random
import Foundation
import SpriteKit

class AKObstacle: SKSpriteNode {

    let Obstacle_Width: CGFloat = 40.0
    let Obstacle_Height: CGFloat = 70.0
    let Obstacle_Color = UIColor.blackColor()

    init(){

        let size = CGSizeMake(Obstacle_Width, Obstacle_Height)
        super.init(texture: nil, color: Obstacle_Color, size: size)

        loadPhysicsBoadyWithSize(size)
        startMoving()

    }

3 个答案:

答案 0 :(得分:1)

一种解决方案是将randomcolor作为静态函数并将其称为类方法。另一个(可能更好,因为该函数似乎并没有特别链接到该类)是使它成为一个公共方法。还有其他人建议在构造函数中调用它,这也有效。

解决方案1 ​​

import UIKit
import Foundation
import SpriteKit

class AKObstacle: SKSpriteNode {

    let Obstacle_Width: CGFloat = 40.0
    let Obstacle_Height: CGFloat = 70.0
    let Obstacle_Color = AKObstacle.getRandomColor()

    init() { ... }

    static func getRandomColor() -> UIColor {

        var randomRed:CGFloat = CGFloat(drand48())

        var randomGreen:CGFloat = CGFloat(drand48())

        var randomBlue:CGFloat = CGFloat(drand48())

        return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
    }
}

解决方案2

import UIKit
import Foundation
import SpriteKit

class AKObstacle: SKSpriteNode {

    let Obstacle_Width: CGFloat = 40.0
    let Obstacle_Height: CGFloat = 70.0
    let Obstacle_Color = getRandomColor()

    init() {...}

}

public func getRandomColor() -> UIColor {

    var randomRed:CGFloat = CGFloat(drand48())

    var randomGreen:CGFloat = CGFloat(drand48())

    var randomBlue:CGFloat = CGFloat(drand48())

    return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}

答案 1 :(得分:0)

你可以说

super.init(texture: nil, color: getRandomColor(), size: size)

答案 2 :(得分:0)

self来电之前,您无法使用super.init。您可以实现UIColor

的扩展名
class MyClass: SKSpriteNode {

    let Obstacle_Width: CGFloat = 40.0
    let Obstacle_Height: CGFloat = 70.0

    init(){

        let size = CGSizeMake(Obstacle_Width, Obstacle_Height)

        super.init(texture: nil, color: UIColor.Obstacle_Color(), size: size)

    }
}

extension UIColor{

    class func Obstacle_Color() -> UIColor {

        var randomRed:CGFloat = CGFloat(drand48())

        var randomGreen:CGFloat = CGFloat(drand48())

        var randomBlue:CGFloat = CGFloat(drand48())

        return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
    }
}