【问题标题】:Kivmob - Interstitial ad not loadingKivmob - 插页式广告未加载
【发布时间】:2020-03-01 15:00:46
【问题描述】:

我一直在尝试添加名为 kivmob 的库以在我的应用程序中显示广告。 Kivmob 加载横幅广告但不加载插页式广告。我对为什么横幅有效但插页式无效感到困惑。我也尝试过使用测试 ID。这是我的 main.py。

from kivmob import KivMob

from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition

from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.utils import platform

from kivymd.uix.button import MDRectangleFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.app import MDApp


class PingPongPaddle(Widget):

    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.05
            ball.velocity = vel.x, vel.y + offset
class PingPongBall(Widget):
    #Velocity on x and y axis
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)

    #So ball.velocity can be used
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    #Moves the ball one step
    def move(self):
        self.pos = Vector(*self.velocity) + self.pos

class PingPongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

    def play(self):
        game = PingPongGame()
        game.serve_ball()
        #Runs every 60th of a second
        Clock.schedule_interval(game.update, 1.0/60.0)
        self.add_widget(game)

    def serve_ball(self, vel=(7, 0)):
        self.ball.center = self.center
        self.ball.velocity = vel
    def update(self, dt):
        self.ball.move()
        #Bounce off the paddles
        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)
        #Bounce off top and bottom
        if (self.ball.y < 0) or (self.ball.top > self.height):
            self.ball.velocity_y *= -1

        #Went off the side
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(7, 0))
        if self.ball.x > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-7, 0))

    def on_touch_move(self, touch):
        #To sense if screen is touched
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y

class GameScreen(Screen):
    def play(self):
        game = PingPongGame()
        game.serve_ball()
        #Runs every 60th of a second
        Clock.schedule_interval(game.update, 1.0/60.0)
        self.add_widget(game)
        ads.new_banner("ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx", top_pos=False)
        ads.request_banner()
        ads.show_banner()

class MainScreen(Screen):
    def show_ad(self):
        ads.new_interstitial("xxxxxxxxxxxxxxxxxxxxxxxxxxx")
        ads.request_interstitial()
        ads.is_interstitial_loaded()
        ads.show_interstitial()
    def on_resume(self):
        self.ads.request_interstitial()




class PingPongApp(MDApp):
    def __init__(self, **kwargs):
        global ads
        self.title = "Ping Pong"
        if platform not in ("android", "ios"):
            Window.size = (1000, 750)
        ads = KivMob("ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxx")

        super().__init__(**kwargs)
    def build(self):
        self.theme_cls.theme_style = "Dark"

        screen_manager = ScreenManager(transition=SlideTransition())
        screen_manager.add_widget(MainScreen(name="main"))
        screen_manager.add_widget(GameScreen(name="game"))

        return screen_manager




if __name__ == "__main__":
    PingPongApp().run()

这是我的 pingpong.kv。

#:kivy 1.11.1

ScreenManager:
    MainScreen:
    GameScreen:

<MainScreen>:
    FloatLayout:
        MDRectangleFlatButton:
            text: "Start!"
            on_touch_down: root.manager.current = "game"
            pos_hint: {"center_x" : 0.5, "center_y" : 0.5}
            size_hint: None, None
            size: 300, 100

<GameScreen>
    on_enter: self.play()

<PingPongBall>:
    size: 75, 75
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size          

<PingPongPaddle>:
    size: 50, 300
    canvas:
        Rectangle:
            pos:self.pos
            size:self.size

<PingPongGame>:

    ball: PingPong_ball
    player1: player_left
    player2: player_right

    canvas:
        Rectangle:
            pos: self.center_x - 5, 0
            size: 20, self.height

    Label:
        font_size: 140  
        center_x: root.width / 4
        top: root.top - 50
        text: str(root.player1.score)

    Label:
        font_size: 140 
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)

    Label:
        font_size: 35
        center_x: root.width * 6/7
        top: root.top - 650
        text: "by me"

    PingPongBall:
        id: PingPong_ball
        center: self.parent.center

    PingPongPaddle:
        id: player_left
        x: root.x
        center_y: root.center_y

    PingPongPaddle:
        id: player_right
        x: root.width - self.width
        center_y: root.center_y

提前致谢!

【问题讨论】:

    标签: python-3.x admob kivy buildozer


    【解决方案1】:

    首先你没有调用主屏幕的 show_ad()。

    您以为屏幕类中的方法在屏幕调用时会自动调用,但事实并非如此。

    你必须在进入屏幕时调用函数。

    为此,您需要更改以下内容:

     # This is your mainscreen class
    class MainScreen(Screen):
        def show_ad(self):
            self.ads.new_interstitial("YOUR_ID_HERE")
            self.ads.request_interstitial()
            self.ads.is_interstitial_loaded()
            self.ads.show_interstitial()
        def on_resume(self):
            self.ads.request_interstitial()
    
        def on_pre_enter(self): 
            self.show_ad() #Functions called on screen 
                                         # invoked
    

    【讨论】:

    • 感谢@tomerpacific 编辑和格式化我的回答代码。我在移动设备上使用网站。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多