控制一个衍生的picturebox(controlling a spawned picturebox)
我试图用c#编写一个小游戏,游戏涉及移动敌人。 这些敌人是使用下面的代码产生的,这个代码被多次使用以产生多个敌人。
private void EventHandler(Action<object, EventArgs> spawnBox) { Random randomPlek = new Random(); int xPlek; xPlek = randomPlek.Next(1000, 1100); int yPlek; yPlek = (randomPlek.Next(0, 8)) * 100; var picture = new PictureBox { Name = "pictureBoxLM", Size = new Size(150, 100), SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent, Location = new Point(xPlek, yPlek), Image = Leeuwenmier, }; this.Controls.Add(picture); }
问题是,当试图使它们移动或碰撞时,Visual Studio找不到名称并出现错误。 这是我用于碰撞的代码:
if(PbMier.Bounds.IntersectsWith(pictureBoxLM.Bounds)) { // some actions }
如何在代码中调用生成的图片框而不会出现错误?
I am trying to make a little game coded in c#, the game involves moving enemies. These enemies are spawned in using the following code, this code is used multiple times to spawn multiple enemies.
private void EventHandler(Action<object, EventArgs> spawnBox) { Random randomPlek = new Random(); int xPlek; xPlek = randomPlek.Next(1000, 1100); int yPlek; yPlek = (randomPlek.Next(0, 8)) * 100; var picture = new PictureBox { Name = "pictureBoxLM", Size = new Size(150, 100), SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent, Location = new Point(xPlek, yPlek), Image = Leeuwenmier, }; this.Controls.Add(picture); }
The problem is that when trying to make them move or collide, Visual Studio can't find the name and gives an error. This is the code i used for collision:
if(PbMier.Bounds.IntersectsWith(pictureBoxLM.Bounds)) { // some actions }
How can I call the spawned picturebox in the code without getting an error?
原文:https://stackoverflow.com/questions/50907741
最满意答案
如果您当前正在键入由Xcode生成的
AppDelegate
类,则self
指的是AppDelegate
类的一个实例。 默认情况下,AppDelegate
的实例没有view
属性,除非您明确添加一个。 您可以尝试应用程序委托的根视图控制器的view
属性。self.window.rootViewController.view.backgroundColor = [UIColor redColor];
这假定您已将
UIViewController
的子类的有效实例分配给您的应用程序委托的根视图控制器。If you are currently typing in the
AppDelegate
class generated by Xcode,self
refers to an instance of theAppDelegate
class. Instances ofAppDelegate
by default do not have aview
property unless you explicitly add one. You could try the app delegate's root view controller'sview
property.self.window.rootViewController.view.backgroundColor = [UIColor redColor];
This assumes that you have assigned a valid instance of a subclass of
UIViewController
to your app delegate's root view controller.
相关问答
更多-
AppDelegate问题(AppDelegate issues)[2022-07-17]
注意:一般建议 您正在从ViewController引用AppDelegate以获取上下文。 这可以工作,但创建VC对appDelegate的依赖...不是最好的事情:D 两种选择: - 给视图控制器一个var ctx并从appDelegate传递给它 - 创建一个具有CD堆栈的DataStoreManager类。 (这会将此代码移出appDelegate,这是理想的恕我直言 - 设置CD堆栈并不真正属于控制器) 最后:它也归结为个人偏好 NOTE: GENERAL ADVICE You are refer ... -
AppDelegate是什么,如何知道何时使用它?(What is the AppDelegate for and how do I know when to use it?)[2022-11-09]
我通常避免安德鲁使用“应用程序的核心”一词所暗示的设计方法。 我的意思是,我认为你应该避免在中央位置集中太多的东西 - 良好的程序设计通常涉及到通过“关注领域”分离功能。 委托对象是一个对象,当连接到它的对象达到某些事件或状态时,该对象将被通知。 在这种情况下,应用程序委托是在UIApplication对象达到某些状态时接收通知的对象。 在许多方面,它是一种专门的一对一观察者模式。 这意味着AppDelegate的“关注领域”正在处理特殊的UIApplication状态。 其中最重要的是: applicat ... -
在“AppDelegate *”类型的对象上找不到iOS属性(iOS Property not found on object of type 'AppDelegate *')[2023-06-18]
也许AppDelegate类中没有这样的属性。 在您需要的接口声明下的AppDelegate.h中 @property (nonatomic, retain) NSNumber* downHUD; 在您的AppDelegate.m中,您需要具有实现声明 @synthesize downHUD; 通过这种方式,您可以定义访问器(getter和setter)来访问名为downHUD的实例变量。 这个访问者是公开的,你可以这样做 dataStore.downHUD = ... 也许这可能是错误。 但是如果没 ... -
对象是否“重”并不重要,只要您只存储对它的引用(在您的情况下在应用程序委托中)。 创建第二个对象会产生影响,但行 appDelegate.parentViewController = self; 不这样做,它只存储对象的引用。 我知道这不能解答您的直接问题,但我认为您应该继续使用“在app delegate中存储引用”方法。 Whether or not an object is "heavy" does not matter as long as you store only a reference t ...
-
ECSliding,在AppDelegate中引用RightController(ECSliding, referece RightController in AppDelegate)[2021-12-30]
不要静静地给他们提供对方的参考。 相反,当它们是滑动视图控制器的一部分时, self.slidingViewController将是一个有效的引用,您可以根据实际存在的关系进行导航: self.slidingViewController.underLeftViewController 使用它时,您应该检查类并转换引用: LeftViewController *leftController = self.slidingViewController.underLeftViewController; if ( ... -
AppDelegate:意外地发现零,同时展开一个可选值(AppDelegate: unexpectedly found nil while unwrapping an Optional value)[2021-09-27]
发现零可能是为mywebView因为它只会在您的viewcontroller视图加载时初始化。 尝试使用以下代码 func myaction() { let mywebview = UIWebView() let url = URL(string: "http://192.168.1.5/doit") let urlreq = URLRequest(url: url!) mywebview.loadRequest(urlreq) } 或者你可以在myaction()之外初始 ... -
未找到AppDelegate类(AppDelegate class not found)[2020-11-16]
使用这行代码不是一个好主意 @property (nonatomic, retain) AppDelegate *app; 在每一堂课你都需要它。 在需要的地方访问代理应用程序的简单方法是执行以下操作: AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 显然你需要这样做: #import "AppDelegate.h" 在你使用它的班上。 如果你想要一个更干净的方法来做到这一点,你可以在你 ... -
在'AppDelegate *'类型的对象上找不到属性“视图”(Property 'view' not found on object of type 'AppDelegate *')[2022-12-28]
如果您当前正在键入由Xcode生成的AppDelegate类,则self指的是AppDelegate类的一个实例。 默认情况下, AppDelegate的实例没有view属性,除非您明确添加一个。 您可以尝试应用程序委托的根视图控制器的view属性。 self.window.rootViewController.view.backgroundColor = [UIColor redColor]; 这假定您已将UIViewController的子类的有效实例分配给您的应用程序委托的根视图控制器。 If yo ... -
AppDelegate协议(AppDelegate protocol)[2022-02-22]
在myAppDelegate.h文件中,确保您拥有: - (void) transFromMainToGalery; 然后将sharedApplication委托返回的通用应用程序委托转换为特定的应用程序委托类: [ (myAppDelegate *)[[UIApplication sharedApplication] delegate] transFromMainToGalery ]; 还要在使用此消息的各种其他类的标头中包含myAppDelegate.h。 (根据需要将您的app委托类和文件名替换为 ... -
使用AppDelegate?(Using the AppDelegate?)[2022-08-20]
我认为你误解了“无视应用代表”的建议。 考虑到应用程序代表的中心位置,它有时会成为共享所有内容的“倾销场”。 需要一两个国旗? 把它扔进app代理! 需要一个柜台? 没问题,把它放到app委托! 这正是你不应该做的事情:存储应用程序状态是模型所做的事情; 保持应用程序委托。 我不认为建议是丢弃为app委托生成的代码,但是:你需要它来管理应用程序的生命周期。 但是,管理生命周期是您应该使用应用程序委托的唯一方法:您的模型类需要与它完全分离。 I think that you misunderstood the ...