TextField上的隐式强制错误(Error of implicit coercion on a TextField)
我认为我有最常见的动作错误。在下面的代码中,我有一个MovieClip与一些TextFields里面,我想动画他们。当我把类分配给MovieClip我得到这个错误
1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.text:TextField.
。 当我trace
孩子时,我得到[Object TextField]
并且代码工作正常,如果我将它放在第一帧并将其应用于动态文本,那么为什么当我尝试将此代码应用于MovieClip的孩子时出现此错误?是否有机会忘记
import
任何必要的图书馆?我已经使TextFields动态化了,我嵌入了字符并为动画设置了反锯齿。
package AScripts { import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import com.greensock.*; import com.greensock.easing.*; import flupie.textanim.*; public class TextFade extends MovieClip { private var child : Object; public function TextFade( ) { /* for (var i : int = 0; i < numChildren; i++ ) { child = getChildAt( i ); trace( child ); */ child = getChildAt( 0 ); var txtanim:TextAnim = new TextAnim( child ); // <-- Error /* TextAnim expects a TextField as argument */ txtanim.mode = TextAnimMode.RANDOM; txtanim.split = TextAnimSplit.WORDS; txtanim.effects = myEffect; txtanim.start(); } function myEffect( block:TextAnimBlock ) : void { TweenLite.to( block , .5 , {alpha : 0 , delay : Math.random( ) * 1 } ); } } }
更新:我提出了建议更改并工作。
import flash.text.*; private var child : TextField; child = getChildAt( i ) as TextField;
I think i have the most common actionscript error.In the code below i have a MovieClip with some TextFields inside and i want to animate them.When i assign the class to the MovieClip i get this error
1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.text:TextField.
. When itrace
the childs i get[Object TextField]
and the code works fine if i place it on the first frame and apply it to a Dynamic Text so why i get this error when i try to apply this code to the childs of a MovieClip ?Is there a chance to have forgot to
import
any necessary library ?I have made the TextFields dynamic,i have embed the characters and set anti-alias for animation.
package AScripts { import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import com.greensock.*; import com.greensock.easing.*; import flupie.textanim.*; public class TextFade extends MovieClip { private var child : Object; public function TextFade( ) { /* for (var i : int = 0; i < numChildren; i++ ) { child = getChildAt( i ); trace( child ); */ child = getChildAt( 0 ); var txtanim:TextAnim = new TextAnim( child ); // <-- Error /* TextAnim expects a TextField as argument */ txtanim.mode = TextAnimMode.RANDOM; txtanim.split = TextAnimSplit.WORDS; txtanim.effects = myEffect; txtanim.start(); } function myEffect( block:TextAnimBlock ) : void { TweenLite.to( block , .5 , {alpha : 0 , delay : Math.random( ) * 1 } ); } } }
UPDATE : I made the suggestion changes and worked.
import flash.text.*; private var child : TextField; child = getChildAt( i ) as TextField;
原文:https://stackoverflow.com/questions/7985182
最满意答案
一个简短的问题,一个简短的回答:是的,是一个混合
一个例子:
我有一个处理与REST-API连接的类,它被称为APIClient。 在测试中,我想连接到不同的服务器。
在测试目标中,我分类APIClient
#import "ApiClient.h" @interface TestApiClient : ApiClient //… @end @interface TestApiClient () @property (nonatomic, strong, readwrite) NSURL *baseURL; @end @implementation TestApiClient - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters { self.baseURL = [NSURL URLWithString:@"http://localhost:8000/"]; return [super requestWithMethod:method path:path parameters:parameters]; } @end
在单元测试课中,我做了#import的调配
@implementation APIUnitTests +(void)load { client = [[ApiClient alloc ] init]; object_setClass(client, [TestApiClient class]); } //… @end
这个cas是保存的,因为我第一次创建了一个基类的子类,然后用子类替换了后者。 由于子类也是一个基类,这是有效的继承。
a short question, a short answer: yes, isa swizzling
What Makes Objective C Dynamic?, page 66
An example:
I have a class that handles connections to a REST-API, it is called APIClient. In testing I want to connect to a different server.
In the testing target I subclass APIClient
#import "ApiClient.h" @interface TestApiClient : ApiClient //… @end @interface TestApiClient () @property (nonatomic, strong, readwrite) NSURL *baseURL; @end @implementation TestApiClient - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters { self.baseURL = [NSURL URLWithString:@"http://localhost:8000/"]; return [super requestWithMethod:method path:path parameters:parameters]; } @end
In the Unit test class I do the swizzling #import
@implementation APIUnitTests +(void)load { client = [[ApiClient alloc ] init]; object_setClass(client, [TestApiClient class]); } //… @end
This cas is save, as I first created a subclass of an base class and then replaced the latter with the subclass. As the subclass is also a base class, this is valid inheritance.
相关问答
更多-
这应该工作 5.class => Fixnum 5.class.superclass => Integer 您还可以使用祖先方法,它将为您提供所有超类的列表 5.class.ancestors => [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject] this should work 5.class => Fixnum 5.class.superclass => Integer You can also use ...
-
一个简短的问题,一个简短的回答:是的,是一个混合 什么使目标C动态?“,第66页 一个例子: 我有一个处理与REST-API连接的类,它被称为APIClient。 在测试中,我想连接到不同的服务器。 在测试目标中,我分类APIClient #import "ApiClient.h" @interface TestApiClient : ApiClient //… @end @interface TestApiClient () @property (nonatomic, strong, readwri ...
-
你真的需要做这样的事情是非常罕见的,通常这是一个迹象,表明你在一个对象内部唠唠叨叨,以后会回来咬你。 如果真的需要做什么,你可以改变你的包只是为了方法调用来改变SUPER看到的东西,或者通过调用完整的方法名来覆盖方法查找。 { package BaseClass; sub new { bless \my $self, shift; } sub foo { my $self = shift; print "BaseClass::foo()\n"; ...
-
使用来自作为超类实例的对象的子类的方法(Using methods from a subclass on an object that is an instance of the superclass)[2022-12-14]
如果CommonClass来自外部库,您可能希望使用Composition over Inheritance原则将其包装在Adapter Pattern中 。 如果您想要更改正在使用的库,则可以完全控制,并允许您添加dontRepeatYourself()等功能。 public class CommonClassAdapter implements MyAdapter { private final CommonClass common; private final String cache ... -
简短的回答是,这是可能的。 更长的答案是您滥用术语超类和子类 。 你描述的关系不是一种继承关系,而是一种组合关系。 换句话说,您的Player类不会扩展您的Team类。 您的Team类将包含Player的实例。 是的,在这种情况下,您的Team类可以访问这些实例的方法和变量,就像您可以使用任何其他Java类一样。 它可能看起来像这样: class Team{ List
players = new ArrayList (); public void addPlaye ... -
不,通过反射是不可能的,因为使用反射来分析现有代码,而不是改变它。 No, it's not possible through reflection becuase reflection is used to analyze the existing code, not to change it.
-
我不确定你要做什么,也不知道你期望的结果。 但是,由于numbers.Number是一个abc (抽象基类),它意味着只需将它注册到abc ,就可以将自己的类视为其子类之一。 (没有必要像我第一次想到的那样覆盖__new__ ,因为我没有意识到这个数字numbers.Number是一个abc 。) 这就是我的意思: import numbers class Parent(object): # Added only to make script runnable pass class Nu ...
-
class Child(): nmbrMovies = 0 nmbrBooks = 3 class Adult(): nmbrMovies = 2 nmbrBooks = 5 class Member(Child): pass x = Member() Member开始继承Child : print(Member.__bases__) # (
,) 可以将此更改为Adult : Mem ... -
您可以使用此重载ICriteria CreateCriteria(string entityName) : public string CheckString(string s){ if(s == "A") return "EntityA";//entity name from the database else if(s == "B") return "EntityB";//entity name from the database } public vo ...
-
我可能会创建一个静态类来保存Element的“扩展方法”。 这类似于Integer.parseInt(String s); Boolean isTest = MyElement.getBoolean(data.getChild("isTest")); 实施将是 public static class MyElement { public static boolean getBoolean(Element e) { // Do your thing. return e ...