首页 \ 问答 \ 控制一个衍生的picturebox(controlling a spawned picturebox)

控制一个衍生的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
更新时间:2023-04-19 20:04

最满意答案

如果您当前正在键入由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 the AppDelegate class. Instances of AppDelegate by default do not have a view property unless you explicitly add one. You could try the app delegate's root view controller's view 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.

相关问答

更多
  • 注意:一般建议 您正在从ViewController引用AppDelegate以获取上下文。 这可以工作,但创建VC对appDelegate的依赖...不是最好的事情:D 两种选择: - 给视图控制器一个var ctx并从appDelegate传递给它 - 创建一个具有CD堆栈的DataStoreManager类。 (这会将此代码移出appDelegate,这是理想的恕我直言 - 设置CD堆栈并不真正属于控制器) 最后:它也归结为个人偏好 NOTE: GENERAL ADVICE You are refer ...
  • 我通常避免安德鲁使用“应用程序的核心”一词所暗示的设计方法。 我的意思是,我认为你应该避免在中央位置集中太多的东西 - 良好的程序设计通常涉及到通过“关注领域”分离功能。 委托对象是一个对象,当连接到它的对象达到某些事件或状态时,该对象将被通知。 在这种情况下,应用程序委托是在UIApplication对象达到某些状态时接收通知的对象。 在许多方面,它是一种专门的一对一观察者模式。 这意味着AppDelegate的“关注领域”正在处理特殊的UIApplication状态。 其中最重要的是: applicat ...
  • 也许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 ...
  • 不要静静地给他们提供对方的参考。 相反,当它们是滑动视图控制器的一部分时, self.slidingViewController将是一个有效的引用,您可以根据实际存在的关系进行导航: self.slidingViewController.underLeftViewController 使用它时,您应该检查类并转换引用: LeftViewController *leftController = self.slidingViewController.underLeftViewController; if ( ...
  • 发现零可能是为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()之外初始 ...
  • 使用这行代码不是一个好主意 @property (nonatomic, retain) AppDelegate *app; 在每一堂课你都需要它。 在需要的地方访问代理应用程序的简单方法是执行以下操作: AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 显然你需要这样做: #import "AppDelegate.h" 在你使用它的班上。 如果你想要一个更干净的方法来做到这一点,你可以在你 ...
  • 如果您当前正在键入由Xcode生成的AppDelegate类,则self指的是AppDelegate类的一个实例。 默认情况下, AppDelegate的实例没有view属性,除非您明确添加一个。 您可以尝试应用程序委托的根视图控制器的view属性。 self.window.rootViewController.view.backgroundColor = [UIColor redColor]; 这假定您已将UIViewController的子类的有效实例分配给您的应用程序委托的根视图控制器。 If yo ...
  • 在myAppDelegate.h文件中,确保您拥有: - (void) transFromMainToGalery; 然后将sharedApplication委托返回的通用应用程序委托转换为特定的应用程序委托类: [ (myAppDelegate *)[[UIApplication sharedApplication] delegate] transFromMainToGalery ]; 还要在使用此消息的各种其他类的标头中包含myAppDelegate.h。 (根据需要将您的app委托类和文件名替换为 ...
  • 我认为你误解了“无视应用代表”的建议。 考虑到应用程序代表的中心位置,它有时会成为共享所有内容的“倾销场”。 需要一两个国旗? 把它扔进app代理! 需要一个柜台? 没问题,把它放到app委托! 这正是你不应该做的事情:存储应用程序状态是模型所做的事情; 保持应用程序委托。 我不认为建议是丢弃为app委托生成的代码,但是:你需要它来管理应用程序的生命周期。 但是,管理生命周期是您应该使用应用程序委托的唯一方法:您的模型类需要与它完全分离。 I think that you misunderstood the ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)