render text to Sprite.graphics -- as3 -? - actionscript

Because I'm a terrible, bad person who likes to do things differently for no reason, I'd love to be able to do something like mySprite.graphics.drawText(...).
As I understand things, the only way to get text currently is to create a TextField and add it as a child of mySprite. In my particular situation I'd rather not do that.
Any advice appreciated!
ooo

The TextField class inherits the InteractiveObject class which inherits the DisplayObject class. Display objects cannot be displayed unless they are programmed to, which is what addChild does.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html for more information.
Also, from http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/states/AddChild.html -
The AddChild class adds a child display object, such as a component,
to a container as part of a view state. You use this class in the
overrides property of the State class.

Related

Not able to get the video if the stream is created first in OPENTOK

I am basically trying to implement a video conference functionality using opentok.
I have two view controllers.
Class A that has a grey image(to tell user is offline).
It calls setsession from class B to establish the session.
uses ClassADelegate and implements setUserOnlineImage that sets the class A grey image to green.
Class B holds a method useronline.
Has a class method sharedinstance that gives out the singleton instance of the class
viewdidload ->sets a variable type = 2;
setsession ->sets a variable type = 1;
It also has a protocol "ClassADelegate"
Protocol ClassADelegate has method setUserOnlineImage.
Has a callback method session:streamCreated: that is called when a subscriber is created and setupPublisher that publishes the video
The flow is like this.
first Class A calls the setsession from Class B to establish session.
Then when a connect button is clicked the viewdidload is called and then the setupPublisher is called, view is modified loaded and all that.
Now when a subscriber tries to connect session:streamCreated: is called. here when i try to print type value it comes as one, likewise many other variables also become nil which inturn results in just giving the audio and the video isnt seen.
where as if first session:streamCreated: is called (first video is received and then connect is clicked) the flow works fine and the print statement in session:streamCreated: correctly prints type value as 2.
Someone help me figure out whats happening.
I want to know why the type value is getting changed & various other variables become nil. This is preventing the video from showing. Am i missing something? Is any other instance is been taken(but I am using a singleton instance)?
The flow you describe doesn't follow any of the known patterns of how UIViewControllers should behave. Specifically, you shouldn't need to use a singleton instance of a view controller. I think you need to reconsider the architecture, specifically the relationship between these two view controllers.
By the way, the viewDidLoad method is called on the view controller as soon as its view property becomes available, which can be before its on the screen. If the view controller is loading its view from a storyboard or nib, viewDidLoad is called as soon as that view is ready. Otherwise if you are implementing loadView, viewDidLoad is called after that method is finished.
Can you describe what Class A and Class B are trying to accomplish? It sounds like Class A is a view controller for some type of status view that shows a user's online/offline status. Class B sounds like its the OTSessionDelegate as well as the view controller for where the publisher/subscriber views will be placed. Why are these not the same View Controller? (generally view controllers are meant to control a "screenful" of content, unless you are using View Controller Containment). If these two view controllers are not on the screen at the same time, can you use a segue to pass data between them when the transition occurs?
UPDATE:
The additional information is useful for me to give you a recommendation. The thing I'm still uncertain about is if you actually do have these 2 view controllers' views on screen at the same time. This solution should work in both cases.
Outside of a segue, one view controller should not really be calling another view controller's methods directly (so calling setsession as you described is a bad idea). You shouldn't even set one as the delegate of another. At most they should share a Model object to communicate. The OTSession can be seen as a Model object. The challenging limitation is that when using the delegation pattern, only one object (you chose Class B) can be informed of updates. Rather than using the delegation pattern, I think you should use NSNotifications. In order to accomplish this, you should "wrap" the OTSession model in your own model object, setting your own model object as the delegate. Then you can notify both controllers of interesting changes as they happen. I've created a diagram to demonstrate:
In this diagram, all the downward solid arrows are owning references. VideoConference would be your own class and it would implement the OTSessionDelegateProtocol. On initialization, the VideoConference instance would create and own an OTSession instance. When something happens that Class A or Class B need to know about (such as the remote user coming online), VideoConference can send an NSNotification, which both controllers can be observers. Here is a useful article about NSNotifications.

Objective-C way or compiler directive to force a class file include a method without adding a protocol?

I have a custom singleton MyColors class to easily change colors of some buttons easily in many UIViewControllers at the same time.
I put #import "MyColors.h" in every class I need to get color of a button.
Everything works perfectly and setting a the color value in MyColors, that color is applied correctly to where I want.
Now, I want to change colors alive. So I send a NSNotification with name "COLORS_CHANGED".
So every class will observe that and apply colors immediately.
What I want to do is:
Force XCode to remind me that the .mfile that includes MyColors.h must also include colorsChanged method for observing. So, this will prevent me forgetting to add observer
OR:
Add something like a Category or something like that into MyColors.h to do this kind of thing...
What I don't want to do:
I don't want to create a protocol and add it as delegate to header files of every .mfile that includes MyColors.h. Because I can forget that too.. If not, this would be the way ofcourse...
So, I am trying to find an Objective-C way to do this, or a compiler way that works with XCode to do this.
Thank you for sharing your ideas.
Update: I use color values for anything that needs colors not just buttons.
implement this functionality in a base class and make all your UIViewController-s inherit it. i.e - in the base class register for this notification and change the common objects' colors and also call there a virtual method for the inheritors to change their extra objects' colors.
Good luck,

iOS: have a clean code, use static class or singleton

I'm developing a game and inside I have a class called StageViewController.
I noticed that code inside is becoming very very long and dull.
In this class I have controller about gesture, position, animation and it's not easy use static class or singleton class to clean this class.
Is a possible solution to use others viewcontrollers inside this StageViewController to simplify the code?
Example: If in my game I should make an entry of an object that I should color, can I use another viewcontroller (with another class) to make my code inside StageViewController more simplify?
If you have any suggestion for me or link to read you can make me happy ;-)
Typically this indicates that you're storing model information in the view controller. The view controller should only keep track of how to display information. You should move the actual state of the game into model classes. These model classes inherit from NSObject, they are not view controllers.
In a well designed model-view-controller system, you should be able to run your entire game without knowing what the display looks like. Your model should be able to take inputs, update the game state, and provide outputs, regardless of how or whether that information is actually displayed. This kind of thinking improves reusability, and also reduces the complexity of your view controllers.

Object Properties From Custom Classes in iOS

This should be easy - but I am scratching my head over it. Here's the problem:
I have a custom class that I import into my MainViewController.h file. The class contains a bunch of properties and a few methods.
I instantiate an object of this class in the ViewDidLoad section, and can run any of the methods or alter the properties on this object at will... they work fine as long as I stay within the ViewDidLoad section.
I created a button with which I wish to activate one of my custom class methods on this object, but the IBAction section seems out of scope, as it doesn't see the object created in ViewDidLoad at all.
Can someone point me in the right direction to enable these two areas to see eachother?
Thanks in advance!

How to record all of the user's touches in iPhone app

note: This is an expansion (and clarification) of a question I asked yesterday.
I am conducting a research project where I want to record all of the user's touches in the iPhone app. After the experiment, I will be able to download the data and process it in either Excel or (more likely) Matlab and determine how many times they clicked on certain buttons, when they clicked certain buttons, etc. To do this, I would need to know:
a) When they touched
b) Where they touched
c) Which view they touched
The first two are easy, but the third I am having trouble with. I know I can do this to get the reference to the UIView that was touched:
CGPoint locationPoint = [[touches anyObject] locationInView:self];
UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];
However, that will just give me a pointer to the view, not the name of the view that was touched. I could assign each view a tag, but then every time I create a new view I would need to remember to tag it (or, alternatively, log the address of each view when initialized and log it when the view is touched). Subclassing UIView and adding an automatic tag isn't really an option since I'm creating other UIButtons and UISliders and would need to subclass those also, which doesn't seem like a very good solution.
Does anyone know of a clean, easy way to do this?
For "Which view they touched", what information do you need?
Perhaps you could use a category to add a method to UIView. This method would generate a string containing information about the view. Such as:
its type e.g. UIButton etc.
its size and position
the title of the view, if it has one (e.g. the button title)
the parent view type and title
other stuff e.g. is the view enabled, what state it is in. anything you like.
For example: "Type:UIButton Title:"Back" Rect:{3,5,40,25}" or some such string.
This is very clean and gives you quite a lot of information to be going with.
You could add a category to UIView which would then be inherited by all UIView descended objects, although I'm not sure its any more efficient than tagging. Since a category can override methods then you could override init methods for automatic tagging I suppose.
http://macdevelopertips.com/objective-c/objective-c-categories.html
I'm not sure what you mean by the "name" of the view. If you mean the view name in Interface Builder, I don't believe it includes that in the instantiated objects. You could use the Tag attribute which is included, but that's just a number and not a name.

Resources